-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ondřej Kaláb
committed
Feb 10, 2020
1 parent
cbc96e4
commit 3a737c7
Showing
14 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 2 additions & 0 deletions
2
aspnetcoreapp-efcore-inherited-entity-id-problem.sln.DotSettings.user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |