Reputation: 692
I'm trying to setup a one to one or zero relationship
but unable to get it working.
All three objects in my example will inherit BaseLogObject
:
public class BaseLogObject
{
public BaseLogObject()
{
Oid = Guid.NewGuid();
}
[Key]
public virtual Guid Oid { get; set; }
}
Below is AccessLog
, it will be related to the next two:
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
}
Two objects that can relate to 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; }
}
I tried adding public virtual ErrorLog ErrorLog { get; set; }
to AccessLog
, but I receive the following error when I run the application:
{"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."}
...and I don't seem to know how to override
or new
the Oid
property properly.
Attempting to override
does not work for me:
public class AccessLog : BaseLogObject
{
[Key]
[ForeignKey("ErrorLog")]
public override Guid Oid { get; set; } //also tried "new"
//...continue with code reference above
}
If I test the code above by trying to add to the AccessLog
table, I receive the following error
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'.
I was also hoping that I did not need to store ErrorLog (and ExceptionLog) > Oid
as FK in the AccessLog table, but if that is required I can accept it. I believe that I am trying to hard or trying to over-simplify this setup.
Upvotes: 0
Views: 509
Reputation: 2881
One way to achieve your goal is:
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; }
}
Basically I made some changes on the entities and used fluent api for configuration.
Upvotes: 1