Skip to content

Commit

Permalink
the web app
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondřej Kaláb committed Feb 10, 2020
1 parent cbc96e4 commit 3a737c7
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/obj
/bin
/.vs
45 changes: 45 additions & 0 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using aspnetcoreapp_efcore_inherited_entity_id_problem.Data;
using Microsoft.AspNetCore.Mvc;

namespace aspnetcoreapp_efcore_inherited_entity_id_problem.Controllers
{
public class HomeController : Controller
{
private readonly AppDbContext _dbContext;

/// <summary>
/// Constructor
/// </summary>
public HomeController(AppDbContext dbContext)
{
_dbContext = dbContext;
}

public IActionResult Index()
{
// Uncomment the case you would like to test...

// 1st problem:
// This causes error "ArgumentException: An item with the same key has already been added. Key: 1".
_dbContext.Cat.Add(
new Cat
{
Name = "Tom",
});
_dbContext.SaveChanges();

// 2nd problem:
// This inserts new Dog with id 1 and replaces Cat with id 1.
//_dbContext.Dog.Add(
// new Dog
// {
// Name = "Laika"
// });
//_dbContext.SaveChanges();


return View(_dbContext.Animal.ToList());
}
}
}
15 changes: 15 additions & 0 deletions Data/Animal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace aspnetcoreapp_efcore_inherited_entity_id_problem.Data
{
public abstract class AnimalBase : Entity<int>
{
public string Name { get; set; }
}

public class Cat : AnimalBase
{
}

public class Dog : AnimalBase
{
}
}
42 changes: 42 additions & 0 deletions Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace aspnetcoreapp_efcore_inherited_entity_id_problem.Data
{
public class AppDbContext : DbContext
{
public DbSet<AnimalBase> Animal { get; set; }
public DbSet<Cat> Cat { get; set; }
public DbSet<Dog> Dog { get; set; }

/// <summary>
/// Constructor.
/// </summary>
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

// Seed data
modelBuilder.Entity<Cat>().HasData(
new Cat
{
Id = 1,
Name = "Mourek",
},
new Cat
{
Id = 2,
Name = "Líza",
}
);
}
}
}
7 changes: 7 additions & 0 deletions Data/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace aspnetcoreapp_efcore_inherited_entity_id_problem.Data
{
public class Entity<T>
{
public T Id { get; set; }
}
}
20 changes: 20 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace aspnetcoreapp_efcore_inherited_entity_id_problem
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
27 changes: 27 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61907",
"sslPort": 44353
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"aspnetcoreapp_efcore_inherited_entity_id_problem": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
45 changes: 45 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using aspnetcoreapp_efcore_inherited_entity_id_problem.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace aspnetcoreapp_efcore_inherited_entity_id_problem
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase("appDb"));

services.AddControllersWithViews();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbContext context)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});

context.Database.EnsureCreated();
}
}
}
27 changes: 27 additions & 0 deletions Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@using aspnetcoreapp_efcore_inherited_entity_id_problem.Data;

@model IEnumerable<AnimalBase>;

<p>
@Model.Count()
</p>

<table border="1" cellpadding="10">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Type</th>
</tr>
</thead>
<tbody>
@foreach (var obj in Model)
{
<tr>
<td>@obj.Id</td>
<td>@obj.Name</td>
<td>@obj.GetType()</td>
</tr>
}
</tbody>
</table>
9 changes: 9 additions & 0 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
16 changes: 16 additions & 0 deletions aspnetcoreapp-efcore-inherited-entity-id-problem.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>aspnetcoreapp_efcore_inherited_entity_id_problem</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions aspnetcoreapp-efcore-inherited-entity-id-problem.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aspnetcoreapp-efcore-inherited-entity-id-problem", "aspnetcoreapp-efcore-inherited-entity-id-problem.csproj", "{87E6CC8B-FE9D-4B35-AF96-6965950DB682}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{87E6CC8B-FE9D-4B35-AF96-6965950DB682}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87E6CC8B-FE9D-4B35-AF96-6965950DB682}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87E6CC8B-FE9D-4B35-AF96-6965950DB682}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87E6CC8B-FE9D-4B35-AF96-6965950DB682}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F2894353-5A28-4E6F-9B5B-BAF478255113}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String></wpf:ResourceDictionary>

0 comments on commit 3a737c7

Please sign in to comment.