Skip to content

Commit

Permalink
Metadata: Avoid sharing default mappings for entity types
Browse files Browse the repository at this point in the history
Resolves #23981
  • Loading branch information
smitpatel committed Aug 26, 2021
1 parent e5b696d commit 084d89c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static IRelationalModel Create(
private static void AddDefaultMappings(RelationalModel databaseModel, IEntityType entityType)
{
var rootType = entityType.GetRootType();
var name = rootType.HasSharedClrType ? rootType.Name : rootType.ShortName();
var name = rootType.Name;
if (!databaseModel.DefaultTables.TryGetValue(name, out var defaultTable))
{
defaultTable = new TableBase(name, null, databaseModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Query
{
Expand All @@ -13,5 +14,55 @@ protected TestSqlLoggerFactory TestSqlLoggerFactory
protected void ClearLog() => TestSqlLoggerFactory.Clear();

protected void AssertSql(params string[] expected) => TestSqlLoggerFactory.AssertBaseline(expected);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Multiple_different_entity_type_from_different_namespaces(bool async)
{
var contextFactory = await InitializeAsync<Context23981>();
using var context = contextFactory.CreateContext();
//var good1 = context.Set<NameSpace1.TestQuery>().FromSqlRaw(@"SELECT 1 AS MyValue").ToList(); // OK
//var good2 = context.Set<NameSpace2.TestQuery>().FromSqlRaw(@"SELECT 1 AS MyValue").ToList(); // OK
var bad = context.Set<NameSpace1.TestQuery>().FromSqlRaw(@"SELECT cast(null as int) AS MyValue").ToList(); // Exception
}

protected class Context23981 : DbContext
{
public Context23981(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var mb = modelBuilder.Entity(typeof(NameSpace1.TestQuery));

mb.HasBaseType((Type)null);
mb.HasNoKey();
mb.ToTable((string)null);

mb = modelBuilder.Entity(typeof(NameSpace2.TestQuery));

mb.HasBaseType((Type)null);
mb.HasNoKey();
mb.ToTable((string)null);
}
}
}
}

namespace NameSpace1
{
public class TestQuery
{
public int? MyValue { get; set; }
}
}

namespace NameSpace2
{
public class TestQuery
{
public int MyValue { get; set; }
}
}
43 changes: 43 additions & 0 deletions test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,33 @@ public void Can_use_relational_model_with_functions()
Assert.Equal(tvfDbFunction.Parameters.Single().Name, tvfFunction.Parameters.Single().DbFunctionParameters.Single().Name);
}

[ConditionalFact]
public void Default_mappings_does_not_share_tableBase()
{
var modelBuilder = CreateConventionModelBuilder();
modelBuilder.Entity<NameSpace1.SameEntityType>().HasNoKey().ToTable((string)null);
modelBuilder.Entity<NameSpace2.SameEntityType>().HasNoKey().ToTable((string)null);

var model = Finalize(modelBuilder);

Assert.Equal(2, model.Model.GetEntityTypes().Count());
Assert.Empty(model.Tables);
Assert.Empty(model.Views);
Assert.Empty(model.Functions);
Assert.Empty(model.Queries);

var entityType1 = model.Model.FindEntityType(typeof(NameSpace1.SameEntityType));
var entityType2 = model.Model.FindEntityType(typeof(NameSpace2.SameEntityType));

var defaultMapping1 = Assert.Single(entityType1.GetDefaultMappings());
var defaultMapping2 = Assert.Single(entityType2.GetDefaultMappings());

Assert.NotSame(defaultMapping1, defaultMapping2);

Assert.True(defaultMapping1.Table.Columns.Single().IsNullable);
Assert.False(defaultMapping2.Table.Columns.Single().IsNullable);
}

private static IRelationalModel Finalize(TestHelpers.TestModelBuilder modelBuilder)
=> modelBuilder.FinalizeModel(designTime: true).GetRelationalModel();

Expand Down Expand Up @@ -994,3 +1021,19 @@ private class Address
}
}
}

namespace NameSpace1
{
public class SameEntityType
{
public int? MyValue { get; set; }
}
}

namespace NameSpace2
{
public class SameEntityType
{
public int MyValue { get; set; }
}
}

0 comments on commit 084d89c

Please sign in to comment.