EntityFramework FluentAPI 映射问题
我有以下代码:
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"));
数据库不遵守约定,FK 不是 Pointer_EntityDef2pId 是否有可能在我的类中只有 1 个属性(EntityDef2 类型的 Pointer_EntityDef2),而不是 id 的 1 个属性和对象的 1 个属性(这个可以正常工作),并且属性具有 FK 名称?映射应该是什么样子?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个流畅的 API 为您提供了所需的模式(无需在 IsIndependent() 之后调用 Map()):
This fluent API gives you the desired schema (no need to call Map() after IsIndependent()):