EF different mapping for an entity based on inheritance - entity-framework

I have two entities (tables) Action and ActionLog. The ActionLog is derived from Action. I need to map entity Action to table Action when it is used alone and map it to table ActionLog when it is used inside an inheritance relationship.
Entities:
Action entity properties:
Action_Property1
Action_Property2
Action_Property3
ActionLog entity properties:
All the inherited properties from Action entity
ActionLog_Property1
Tables:
Action table columns:
Action_Property1
Action_Property2
Action_Property3
ActionLog table columns:
Action_Property1
Action_Property2
Action_Property3
ActionLog_Property1
Is this possible using EF6 Code First mapping in a single context?
Edit 1:
I try to be more explicit. I need something like this:
using(var ctx = new DbContext())
{
var action = new Action
{
Action_Property1 = 1,
Action_Property2 = 2,
Action_Property3 = 3
};
ctx.Actions.Add(action);
ctx.SaveChanges();
}
The lines above should write the Action entity to Action table.
using(var ctx = new DbContext())
{
var actionLog = new ActionLog
{
Action_Property1 = 1,
Action_Property2 = 2,
Action_Property3 = 3,
ActionLog_Property1 = 1
};
ctx.ActionLogs.Add(actionLog);
ctx.SaveChanges();
}
The lines above should write the ActionLog entity to ActionLog table.

Yes, it's possible. Using Mapping the Table-Per-Concrete Class (TPC) Inheritance
it can do so
public class InheritanceMappingContext : DbContext
{
public DbSet<Action> Action { get; set; }
public DbSet<ActionLog> ActionLog { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ActionLog>().Map(m =>
{
m.MapInheritedProperties(); // add inherited property to actionLog
m.ToTable("ActionLog");
});
modelBuilder.Entity<Action>().Map(m =>
{
m.ToTable("Action");
});
}
}

Sure, map your base class:
public class YourBaseClassMapping<T> : EntityTypeConfiguration<T> where T : YourBaseClassEntity
{
protected YourBaseClassMapping()
{
HasKey(x => x.Id);
...
}
}
and then map inherited class:
public class InheritedClassMapping<T> : EntityTypeConfiguration<T> where T : InheritedClassMapping
{
public InheritedClassMapping()
{
// new mapping for inherited class
}
}
and add both mappings to DbModelBuilder as other mappings:
public class YourDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new YourBaseClassMapping());
modelBuilder.Configurations.Add(new InheritedClassMapping());
}
}

Related

Entity Framework Core (6.0.8) indirect Many to Many relationship with 2 db contexts

I am unable to generate a migration for a many to many relationship that spans 2 different db contexts.
I have 3 entities:
AccountApp, SubscriptionDetail, SubscribedAccountApp
AccountApp and SubscribedAccountApp are the AccountDbContext while the SubscriptionDetail is in a SubscriptionsDbContext.
I want to set up an indirect many to many relationship as described here
Based on the documentation, I have created an EntityTypeConfig for the SubscribedAccountApp like this
public class SubscribedAccountAppConfig : IEntityTypeConfiguration<SubscribedAccountApp>
{
public void Configure(EntityTypeBuilder<SubscribedAccountApp> builder)
{
builder.ToTable("subscribed_account_apps");
builder.HasKey(m => new { m.SubscriptionDetailId, m.AccountAppId });
builder.HasOne(x => x.SubscriptionDetail)
.WithMany(x => x.SubscribedAccountApps)
.HasForeignKey(x => x.SubscriptionDetailId);
builder.HasOne(x => x.AccountApp)
.WithMany(x => x.SubscribedAccountApps)
.HasForeignKey(x => x.AccountAppId);
}
}
This config is then applied to the AccountDbContext like so
public class AccountDbContext : DbContext
{
public IQueryable<AccountApp> AccountApps { get; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AccountAppConfig());
modelBuilder.ApplyConfiguration(new SubscribedAccountAppConfig());
}
}
The data model for SubsriptionsDetails looks like:
public class SubscriptionDetail
{
private readonly List<SubscribedAccountApp> _subscribedAccountApps;
private readonly List<ConsumptionComponent> _consumptionComponents;
public SubscriptionDetail(
Guid id,
IEnumerable<ConsumptionComponent> consumptionComponents = null,
IEnumerable<SubscribedAccountApp> subscribedAccountApps = null)
{
Id = id;
_consumptionComponents = consumptionComponents ?? new List<ConsumptionComponent>();
_subscribedAccountApps = subscribedAccountApps ?? new List<SubscribedAccountApp>();
}
public Guid Id { get; set; }
public IReadOnlyCollection<ConsumptionComponent> ConsumptionComponents => _consumptionComponents;
public IReadOnlyCollection<SubscribedAccountApp> SubscribedAccountApps => _subscribedAccountApps;
}
The data model for AccountApp looks like:
public class AccountApp
{
public readonly List<SubscribedAccountApp> _subscribedAccountApps;
public AccountApp(Guid id,
Guid accountId,
IEnumerable<SubscribedAccountApp> subscribedAccountApps = null)
{
Id = id;
_subscribedAccountApps = subscribedAccountApps.ToNavigationProperty();
}
public Guid Id { get; private set; }
public IReadOnlyCollection<SubscribedAccountApp> SubscribedAccountApps => _subscribedAccountApps;
}
This leaves me an error when I try to create a migration for the AccountDbContext:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.InvalidOperationException: No suitable constructor was found for entity type 'ConsumptionComponent'.
This error describes a ConsumptionComponent constructor for some reason, although this model has already existed and nothing has changed on that model. This model does appear as an IEnumerable in the SubscriptionDetail constructor but I am not sure why it is showing up as an error. All I want this migration to do is add support for the indirect-many-to-many-relationship which has nothing to do with ConsumptionComponent.

Entity Framework CodeFirst, Add Dbset to DbContext, programmatically

how can i Add DbSet to my dbContext class, programmatically.
[
public class MyDBContext : DbContext
{
public MyDBContext() : base("MyCon")
{
Database.SetInitializer<MyDBContext>(new CreateDatabaseIfNotExists<MyDBContext>());
}
//Do this part programatically:
public DbSet<Admin> Admins { get; set; }
public DbSet<MyXosh> MyProperty { get; set; }
}
][1]
i want to add my model classes by ((C# Code-DOM)) and of course i did. but now i have problem with creating DbSet properties inside my Context class ...
yes i did!..
this: https://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/
And this: Create Table, Run Time using entity framework Code-First
are solution. no need to dispute with dbSets directly. it just works by do some thing like that:
public class MyDBContext : DbContext
{
public MyDBContext() : base("MyCon")
{
Database.SetInitializer<MyDBContext>(new CreateDatabaseIfNotExists<MyDBContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
var theList = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.Namespace == "FullDynamicWepApp.Data.Domins")
.ToList();
foreach (var item in theList)
{
entityMethod.MakeGenericMethod(item)
.Invoke(modelBuilder, new object[] { });
}
base.OnModelCreating(modelBuilder);
}
}
For those using EF Core that stubble here:
The code below is only for one table with the generic type. If you want more types you can always pass them through the constructor and run a cycle.
public class TableContextGeneric<T> : DbContext where T : class
{
private readonly string _connectionString;
//public virtual DbSet<T> table { get; set; }
public TableContextGeneric(string connectionString)
{
_connectionString = connectionString;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var entityMethod = typeof(ModelBuilder).GetMethods().First(e => e.Name == "Entity");
//the cycle will be run here
entityMethod?.MakeGenericMethod(typeof(T))
.Invoke(modelBuilder, new object[] { });
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString); // can be anyone
}
}

Breeze EF SaveChanges() on a DTO

I've been struggling with Breeze to SaveChanges to a projection and admit I new to both EF and breeze. There were some similar questions earlier when I was trying to use WCF, but now I have abandoned WCF and added EF directly to my solution.
In my controller I return the DTO for the metadata to breeze along with the DTO and it binds perfectly.
After altering the data on the client my Breese Controllers [HttpPost] SaveChanges(save Bundle) is called and map contains the DTO and the changes.
How Do I persist the Changes? If I re-read the DTO projection for breeze to update then EF cant save a projection because it's not "tracked", if I read the Full entity, then Breeze error with "Sequence contains no matching element" because its looking for the DTO? Am I suppose to use AutoMapper?
Controller:
[BreezeController]
public class BreezeController : ApiController
{
readonly EFContextProvider<ManiDbContext> _contextProvider = new EFContextProvider<ManiDbContext>();
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
[HttpGet]
public IQueryable<ConsigneDTO> Consignee(string refname)
{
return _contextProvider.Context.consigneDTO(refname);
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
ManiDbContextProvider _mcontextProvider = new ManiDbContextProvider();
return _mcontextProvider.SaveChanges(saveBundle);
}
ManiDbContext (the main DBContext is CifContext which is Database First/EF Reverse Engineer)
public class ManiDbContext : DbContext
{
public CifContext CifDbContext = new CifContext();
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
Database.SetInitializer<ManiDbContext>(null);
modelBuilder.Configurations.Add(new ConsigneDTOMap());
}
public override int SaveChanges()
{
CifDbContext.SaveChanges();
return 1;
}
public IQueryable<ConsigneDTO> consigneDTO(string refname)
{
IQueryable<ConsigneDTO> q = this.CifDbContext.Consignes
.Where(x => x.Refname == refname)
.Select(f => new ConsigneDTO {Refname = f.Refname, Consignee = f.Consignee, Address1 = f.Address1, Address2 = f.Address2, Address3 = f.Address3});
return q;
}
ManiDbContextProvider
public class ManiDbContextProvider : EFContextProvider<CifContext>
// public class ManiDbContextProvider : EFContextProvider<ManiDbContext>
{
public ManiDbContextProvider() : base() { }
protected override void OpenDbConnection()
{// do nothing
}
protected override void CloseDbConnection()
{ // do nothing
}
protected override bool BeforeSaveEntity(EntityInfo entityInfo)
{
var entity = entityInfo.Entity;
if (entity is ConsigneDTO)
{
return BeforeSaveConsignee(entity as ConsigneDTO, entityInfo);
}
throw new InvalidOperationException("Cannot save entity of unknown type");
}
private bool BeforeSaveConsignee(ConsigneDTO c, EntityInfo info)
{
var consdata = this.Context.CifDbContext.Consignes
.Where(x => x.Refname == c.Refname)
.FirstOrDefault(); // ENTITY
// var consdata = this.Context.consigneDTO(c.Refname); // DTO
return (null != consdata) || throwCannotFindConsignee();
}
CifContext (Full Columns - First/EF Reverse Engineer/ Consigne class contains Keys)
public partial class CifContext : DbContext
{
static CifContext()
{
Database.SetInitializer<CifContext>(null);
}
public CifContext()
: base("Name=CifContext")
{
}
public DbSet<Consigne> Consignes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); // Use singular table names
Database.SetInitializer<CifContext>(null);
modelBuilder.Configurations.Add(new ConsigneMap());
}
Regardless if I Read the Entity or the DTO - I'm Clueless on how breeze updates EF
Any Help greatly appreciated :)
Regards,
Mike

Entity Framework - TPC - Override Primary Key Column Name

Here's my model:
public abstract class Entity
{
public long Id { get; set; }
}
public abstract class Audit : Entity
{}
public class UserAudit : Audit
{
public virtual User User { get; set; }
}
public class User : Entity
{}
Here's my DbContext:
public class TestDbContext : DbContext
{
static TestDbContext()
{
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AuditConfiguration());
modelBuilder.Configurations.Add(new UserAuditConfiguration());
modelBuilder.Configurations.Add(new UserConfiguration());
}
}
And here's my mappings:
public abstract class EntityConfiguration<T> : EntityTypeConfiguration<T>
where T : Entity
{
protected EntityConfiguration()
{
HasKey(t => t.Id);
Property(t => t.Id)
.HasColumnName("Key");
}
}
public class AuditConfiguration : EntityConfiguration<Audit>
{}
public class UserAuditConfiguration : EntityTypeConfiguration<UserAudit>
{
public UserAuditConfiguration()
{
Map(m =>
{
m.MapInheritedProperties();
m.ToTable("UserAudits");
});
HasRequired(u => u.User)
.WithMany()
.Map(m => m.MapKey("UserKey"));
}
}
public class UserConfiguration : EntityConfiguration<User>
{}
When I try to generate a migration for this model, I get the following error:
error 2010: The Column 'Id' specified as part of this MSL does not exist in MetadataWorkspace.
If I comment out the ".HasColumnName" call in the constructor of EntityConfiguration, the migration generates correctly (except of course that the column name is Id and not Key).
Is TPC mappings supposed to support primary key columns that don't use the default column name?
The problem appears to be that Entity Framework does not expect me to map Audit. If I remove AuditConfiguration, this works as expected.

EF CTP5 failing to update optional navigation property

I have a many to one association between "Project" and "Template".
Project has a property of type "Template".
The association is not bidirectional ("Template" has no knowledge of "Project").
My entity mapping for the association on "Project" is:
this.HasOptional(p => p.Template);
If I create a "Project" without specifying a template then null is correctly inserted into the "TemplateId" column of the "Projects" table.
If I specify a template then the template's Id is correctly inserted. The SQL generated:
update [Projects]
set [Description] = '' /* #0 */,
[UpdatedOn] = '2011-01-16T14:30:58.00' /* #1 */,
[ProjectTemplateId] = '5d2df249-7ac7-46f4-8e11-ad085c127e10' /* #2 */
where (([Id] = '8c1b2d30-b83e-4229-b0c3-fed2e36bf396' /* #3 */)
and [ProjectTemplateId] is null)
However, if I try to change the template or even set it to null, the templateId is not updated. The SQL generated:
update [Projects]
set [UpdatedOn] = '2011-01-16T14:32:14.00' /* #0 */
where ([Id] = '8c1b2d30-b83e-4229-b0c3-fed2e36bf396' /* #1 */)
As you can see, TemplateId is not updated.
This just does not make sense to me. I have even tried explicitly setting the "Template" property of "Project" to null in my code and when stepping through the code you can see it has absolutely no effect!
Thanks,
Ben
[Update]
Originally I thought this was caused by me forgetting to add the IDbSet property on my DbContext. However, now that I've tested it further I'm not so sure. Below is a complete test case:
public class PortfolioContext : DbContext, IDbContext
{
public PortfolioContext(string connectionStringName) : base(connectionStringName) { }
public IDbSet<Foo> Foos { get; set; }
public IDbSet<Bar> Bars { get; set; }
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {
modelBuilder.Configurations.Add(new FooMap());
modelBuilder.Configurations.Add(new BarMap());
base.OnModelCreating(modelBuilder);
}
public new IDbSet<TEntity> Set<TEntity>() where TEntity : class {
return base.Set<TEntity>();
}
}
public class Foo {
public Guid Id { get; set; }
public string Name { get; set; }
public virtual Bar Bar { get; set; }
public Foo()
{
this.Id = Guid.NewGuid();
}
}
public class Bar
{
public Guid Id { get; set; }
public string Name { get; set; }
public Bar()
{
this.Id = Guid.NewGuid();
}
}
public class FooMap : EntityTypeConfiguration<Foo>
{
public FooMap()
{
this.ToTable("Foos");
this.HasKey(f => f.Id);
this.HasOptional(f => f.Bar);
}
}
public class BarMap : EntityTypeConfiguration<Bar>
{
public BarMap()
{
this.ToTable("Bars");
this.HasKey(b => b.Id);
}
}
And the test:
[Test]
public void Template_Test()
{
var ctx = new PortfolioContext("Portfolio");
var foo = new Foo { Name = "Foo" };
var bar = new Bar { Name = "Bar" };
foo.Bar = bar;
ctx.Set<Foo>().Add(foo);
ctx.SaveChanges();
object fooId = foo.Id;
object barId = bar.Id;
ctx.Dispose();
var ctx2 = new PortfolioContext("Portfolio");
var dbFoo = ctx2.Set<Foo>().Find(fooId);
dbFoo.Bar = null; // does not update
ctx2.SaveChanges();
}
Note that this is using SQL CE 4.
Ok, you just need to load the navigation property before doing anything to it. By loading it you essentially register it with ObjectStateManager which EF looks into to generate the update statement as a result of SaveChanges().
using (var context = new Context())
{
var dbFoo = context.Foos.Find(fooId);
((IObjectContextAdapter)context).ObjectContext.LoadProperty(dbFoo, f => f.Bar);
dbFoo.Bar = null;
context.SaveChanges();
}
This code will result in:
exec sp_executesql N'update [dbo].[Foos]
set [BarId] = null
where (([Id] = #0) and ([BarId] = #1))
',N'#0 uniqueidentifier,#1 uniqueidentifier',#0='A0B9E718-DA54-4DB0-80DA-C7C004189EF8',#1='28525F74-5108-447F-8881-EB67CCA1E97F'
If this is a bug in EF CTP5 (and not my code :p) there are two workarounds that I came up with.
1) Make the association Bi-Directional. In my case this meant adding the following to my ProjectTemplate class:
public virtual ICollection<Project> Projects {get;set;}
With this done, in order to set the "Template" property of project to null, you can just remove the project from the template - a little backward but it works:
var project = repo.GetById(id);
var template = project.Template;
template.Projects.Remove(project);
// save changes
2) The second option (which I preferred but it still smells) is to add the foreign key on your domain object. In my case I had to add the following to Project:
public Guid? TemplateId { get; set; }
public virtual ProjectTemplate Template { get; set; }
Make sure the Foreign key is a nullable type.
I then had to change my mapping like so:
this.HasOptional(p => p.Template)
.WithMany()
.HasForeignKey(p => p.TemplateId);
Then, in order to set the Template to null, I added a helper method to Project (it does actually work just by setting the foreign key to null):
public virtual void RemoveTemplate() {
this.TemplateId = null;
this.Template = null;
}
I can't say that I'm happy about polluting my domain model with foreign keys but I couldn't find any alternatives.
Hope this helps.
Ben