![](/img/trans.png)
[英]Entity Framework Fluent API relationship mapping HasRequired().WithRequired() not behaving correctly without Map()
[英]Entity framework WithRequired fluent api to data annotations mapping
我正在尝试在我的代码优先实体框架项目中使用数据注释方法。
这是我的实体:(显示有限的字段)
public partial class CUSTOMEREXT
{
[StringLength(36)]
public string ID { get; set; }
public virtual CUSTOMER CUSTOMER { get; set; }
}
public partial class CUSTOMER
{
[StringLength(36)]
public string ID { get; set; }
public virtual CUSTOMEREXT CUSTOMEREXT { get; set; }
}
Fluent API:(这有效)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<CUSTOMER>()
.Property(e => e.ID)
.IsFixedLength()
.IsUnicode(false);
modelBuilder.Entity<CUSTOMER>()
.HasOptional(e => e.CUSTOMEREXT)
.WithRequired(e => e.CUSTOMER);
modelBuilder.Entity<CUSTOMEREXT>()
.Property(e => e.ID)
.IsFixedLength()
.IsUnicode(false);
}
动态生成模型构建器:(这不起作用)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.AddFromAssembly(Assembly.GetAssembly(GetType())); //Current Assembly
base.OnModelCreating(modelBuilder);
}
测试代码:
Model1 model = new Model1();
var outp = model.Set<CUSTOMEREXT>().ToList();
var out1p = model.Set<CUSTOMER>().ToList();
错误:
无法确定类型“OraclePOC.CUSTOMER”和“OraclePOC.CUSTOMEREXT”之间关联的主体端。 必须使用关系流畅 API 或数据注释显式配置此关联的主体端。
通过查看这个,我知道我必须将WithRequired
转换为数据注释属性。 不确定如何?
任何的想法?
我能够弄清楚这一点:
添加[Required]
属性:
public partial class CUSTOMEREXT
{
[StringLength(36)]
public string ID { get; set; }
[Required]
public virtual CUSTOMER CUSTOMER { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.