問題描述
EntityFramework FluentAPI 映射問題 (EntityFramework FluentAPI mapping issue)
I have the following code:
namespace DynamicAssembly {
using System;
using System.Collections.Generic;
using System.Collections;
using System.Data.Objects;
using System.Data.EntityClient;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.Infrastructure;
[Table("eElementDef1")]
public class ElementDef1 {
public int pID { get; set; }
public virtual ElementDef2 Pointer_EntityDef2 { get; set; }
public virtual ElementDef1 Poniter_EntityDef1 { get; set; }
public ElementDef1() {
}
}
[Table("eElementDef2")]
public class ElementDef2 {
public int pID { get; set; }
public String Name { get; set; }
public ElementDef2() {
}
}
public class Context : System.Data.Entity.DbContext {
public DbSet<ElementDef1> ElementDef1
{
get;set;
}
public DbSet<ElementDef2> ElementDef2
{
get;set;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID);
modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2)
.WithMany()
.IsIndependent()
.Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2"));
modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1)
.WithMany()
.IsIndependent()
.Map(m => m.MapKey(p => p.pID, "Poniter_EntityDef1"));
modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID);
}
public Context() :
base("DynamicDefinitionConnection") {
}
}
}
the problem is with this mapping modelBuilder.Entity().HasRequired(p => p.Pointer_EntityDef2) .WithMany() .IsIndependent() .Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2"));
The db does not respect de convention, the FK is not Pointer_EntityDef2pId Is it possible that in my class to have only 1 property (Pointer_EntityDef2 of type EntityDef2) instead of 1 property for the id and one for the object (this one works ok), and the property to have the FK name? And how should the mapping look like?
參考解法
方法 1:
This fluent API gives you the desired schema (no need to call Map() after IsIndependent()):
[Table("eElementDef1")]
public class ElementDef1
{
public int pID { get; set; }
public virtual ElementDef2 Pointer_EntityDef2 { get; set; }
public virtual ElementDef1 Poniter_EntityDef1 { get; set; }
}
[Table("eElementDef2")]
public class ElementDef2
{
public int pID { get; set; }
public String Name { get; set; }
}
public class StackoverflowContext : DbContext
{
public DbSet<ElementDef1> ElementDef1s { get; set; }
public DbSet<ElementDef2> ElementDef2s { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID);
modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID);
modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2)
.WithMany()
.IsIndependent();
modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1)
.WithMany()
.IsIndependent();
}
}
(by bogdanbrudiu、Morteza Manavi)