实体框架一对一或零关系

时间:2018-02-19 18:51:14

标签: c# entity-framework one-to-one

我正在尝试设置int num = 7; for (double y = 2; y >= 0; y-=0.2) { for (double x = 0; x <= num; x+=0.2) { if ( ((0.1+x) >= Math.asin((double)y-1)) && (((double)x-0.1) <= Math.asin((double)y-1)) ) System.out.print('*'); else System.out.print(' '); } System.out.println(); } 但无法让它正常工作。

我的示例中的所有三个对象都将继承one to one or zero relationship

BaseLogObject

以下是public class BaseLogObject { public BaseLogObject() { Oid = Guid.NewGuid(); } [Key] public virtual Guid Oid { get; set; } } ,它与下两个相关:

AccessLog

两个与public class AccessLog : BaseLogObject { [Display(Name = "Ip Address")] public string IpAddress { get; set; } //My attempt to create a one to one or zero public virtual ErrorLog ErrorLog { get; set; } //I only ever plan to have a one to one, //so that "many" would be overkill and not needed //public virtual IList<ErrorLog> ErrorLog { get; set; } //ExceptionLog properties will be here once I get ErrorLog working } 相关的对象:

AccessLog

我尝试将public class ErrorLog : BaseLogObject { public string Error { get; set; } [Display(Name = "Access Log")] public Guid? AccessLogID { get; set; } [ForeignKey("AccessLogID")] public virtual AccessLog AccessLog { get; set; } } public class ExceptionLog : BaseLogObject { public string Exception { get; set; } [Display(Name = "Access Log")] public Guid? AccessLogID { get; set; } [ForeignKey("AccessLogID")] public virtual AccessLog AccessLog { get; set; } } 添加到public virtual ErrorLog ErrorLog { get; set; },但在运行应用程序时收到以下错误:

AccessLog

...而且我似乎不知道如何{"Unable to determine the principal end of an association between the types 'x.Entities.Logs.ErrorLog' and 'x.Entities.Logs.AccessLog'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."} override new属性。

尝试Oid对我不起作用:

override

如果我通过尝试添加到public class AccessLog : BaseLogObject { [Key] [ForeignKey("ErrorLog")] public override Guid Oid { get; set; } //also tried "new" //...continue with code reference above } 表来测试上面的代码,则会收到以下错误

AccessLog

我也希望我不需要存储ErrorLog(和ExceptionLog)&gt; The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AccessLog_dbo.ErrorLog_Oid". The conflict occurred in database "dbName", table "dbo.ErrorLog", column 'Oid'. 在AccessLog表中作为FK,但如果需要,我可以接受它。我相信我正在努力或试图过度简化这种设置。

1 个答案:

答案 0 :(得分:1)

实现目标的一种方法是:

 public class BaseLogContext : DbContext
{
    public DbSet<AccessLog> AccessLogs { get; set; }
    public DbSet<ErrorLog> ErrorLogs { get; set; }
    public DbSet<ExceptionLog> ExeptionLogs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ErrorLog>()
            .HasRequired(x => x.AccessLog)
            .WithOptional(x => x.ErrorLog);

        modelBuilder.Entity<ExceptionLog>()
            .HasRequired(x => x.AccessLog)
            .WithOptional(x => x.ExceptionLog);

        modelBuilder.Entity<ErrorLog>()
            .HasKey(x => x.AccessLogID);
        modelBuilder.Entity<ExceptionLog>()
           .HasKey(x => x.AccessLogID);

        base.OnModelCreating(modelBuilder);
    }
}

public abstract class BaseLogObject
{
    public BaseLogObject()
    {
        Id = Guid.NewGuid();
    }
    public virtual Guid Id { get; set; }
}

public class AccessLog : BaseLogObject
{
    [Display(Name = "Ip Address")]
    public string IpAddress { get; set; }

    public virtual ErrorLog ErrorLog { get; set; }
    public virtual ExceptionLog ExceptionLog { get; set; }
}

public class ErrorLog 
{
    public string Error { get; set; }
    [Display(Name = "Access Log")]
    public Guid AccessLogID { get; set; }
    public virtual AccessLog AccessLog { get; set; }
}

public class ExceptionLog 
{
    public string Exception { get; set; }

    [Display(Name = "Access Log")]
    public Guid AccessLogID { get; set; }
    public virtual AccessLog AccessLog { get; set; }
}

基本上我对实体做了一些更改,并使用流畅的api进行配置。