我有以下代码:
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") {
}
}
}
问题在于这个映射 modelBuilder.Entity().HasRequired(p => p.Pointer_EntityDef2) .WithMany() .IsIndependent() .Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2") );
db 不遵守 de 约定,FK 不是 Pointer_EntityDef2pId 是否有可能在我的类中只有 1 个属性(EntityDef2 类型的 Pointer_EntityDef2)而不是 1 个属性用于 id 和一个用于对象(这个工作正常),以及具有 FK 名称的属性?映射应该是什么样子?