开发者

Cyclic reference error in EF code first

This is my abstract base class:

publi开发者_JS百科c abstract class ServiceStation
    {
        #region '----- Member(s) -----'

        public int Id { get; set; }

        public int CompanyId { get; set; }

        [ForeignKey("CompanyId")]
        public virtual Company Company { get; set; }

        public int GasolineBrandId { get; set; }

        [ForeignKey("GasolineBrandId")]
        public virtual GasolineBrand GasolineBrand { get; set; }

        [ForeignKey("LastUpdatedByUserId")]
        public virtual User LastUpdatedBy { get; set; }

        public int LastUpdatedByUserId { get; set; }

        [ForeignKey("CreatedByUserId")]
        public virtual User CreatedBy { get; set; }

        public int CreatedByUserId { get; set; }

        #endregion
    }

I've inherited this class into CompanyStation:

public class CompanyStation : ServiceStation
    {
        #region '----- Member(s) -----'

        [InverseProperty("CompanyStation")]
        public virtual SapphirePosSystem SapphirePosSystem { get; set; }

        [ForeignKey("StoreManagerId")]
        public virtual User StoreManager { get; set; }

        public int StoreManagerId { get; set; }

        [ForeignKey("OfficeManagerId")]
        public virtual User OfficeManager { get; set; }

        public int OfficeManagerId { get; set; }

        #endregion
    }

When I do that I keep getting error of foreign key cyclic error for StoreManager. If I remove that property then I get error for OfficeManager. Likewise if I remove that I get error for CreatedBy and so on. All foreign keys throw this kind of cyclic reference error. I have no birectional relationship. Don't know why Code First thinks it is cyclic. By trial and error I found that if I put this piece of code in my EntityConfiguration it works fine:

public class CompanyStationConfiguration : EntityTypeConfiguration<CompanyStation>
    {
        #region '----- Methods -----'

        public CompanyStationConfiguration()
            : base()
        {
            HasRequired(e => e.StoreManager).
                    WithMany().
                        HasForeignKey(e => e.StoreManagerId).
                            WillCascadeOnDelete(false);

            HasRequired(e => e.OfficeManager).
                   WithMany().
                       HasForeignKey(e => e.OfficeManagerId).
                           WillCascadeOnDelete(false);

            HasRequired(e => e.Company).
                   WithMany().
                       HasForeignKey(e => e.CompanyId).
                           WillCascadeOnDelete(false);

            HasRequired(e => e.LastUpdatedBy).
                WithMany().
                    HasForeignKey(e => e.LastUpdatedByUserId).
                        WillCascadeOnDelete(false);

            HasRequired(e => e.CreatedBy).
            WithMany().
                HasForeignKey(e => e.CreatedByUserId).
                    WillCascadeOnDelete(false);

            Map(e => { e.MapInheritedProperties(); e.ToTable("CompanyStations"); });
        }

Can anybody tell me why is this happening and have I solved the problem correctly or it is a bit of patch work?

Thanks in advance :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜