我试图建立一个与零关系的关系,我以为我已经解决了,但是当我进一步测试时我错了。我显然不太了解FluentAPI,但我也看到很多人遇到了我遇到的相同或类似的问题
以下是我的课程:
public class BaseLogObject
{
public BaseLogObject()
{
Oid = Guid.NewGuid();
Date = DateTime.Now;
}
[Key]
[Column(Order = 0)]
public Guid Oid { get; set; }
[ScaffoldColumn(true)]
[Display(Name = "Date")]
[Required]
[Column(Order = 1)]
public DateTime Date { get; set; }
}
public class AccessLog : BaseLogObject
{
public virtual ErrorLog ErrorLog { get; set; }
}
public class ErrorLog : BaseLogObject
{
//Hoping to keep this property, but .MapKey give "Unique" error
[Display(Name = "Access Log")]
public virtual Guid? AccessLogID { get; set; }
public virtual AccessLog AccessLog { get; set; }
}
FluentAPI设置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ErrorLog>()
.HasOptional(a => a.AccessLog)
.WithOptionalDependent(x => x.ErrorLog)
.Map(a => a.MapKey("AccessLogID"));
//Thought this worked, but AccessLogID became the PrimaryKey
//modelBuilder.Entity<ErrorLog>().HasKey(x => x.AccessLogID);
//modelBuilder.Entity<ErrorLog>().HasRequired(x => x.AccessLog);
//This creates a new column in SQL (AccessLog_Oid)
//modelBuilder.Entity<AccessLog>()
// .HasOptional(pi => pi.ErrorLog)
// .WithOptionalDependent(x => x.AccessLog);
//Tried reversing the relationship, no go
//modelBuilder.Entity<AccessLog>()
// .HasOptional(x => x.ErrorLog)
// .WithRequired(x => x.AccessLog);
base.OnModelCreating(modelBuilder);
}
AccessLogID:Name:类型中的每个属性名称必须是唯一的。物业名称&#39; AccessLogID&#39;已定义。
我理解AccessLogID
是重复的,但希望我可以将其保留在我的ErrorLog
模型中,以便我可以在整个应用程序中引用它(技术上,我在会话中保存Guid)并根据需要记录错误/异常,我试图避免存储整个对象,只有Guid)。