简体   繁体   English

新创建的 EF 代码第一个表的 SQL Server 外键窗口问题

[英]SQL Server foreign key window issue for newly created EF Code first table

I don't know if anyone face this issue or it's just something else.我不知道是否有人面临这个问题,或者只是其他问题。

I am using a EF code-first architecture.我正在使用 EF 代码优先架构。

Here is my first class :这是我的第一堂课:

public class PerformanceSurchargeGeneralSettings : FullAuditedEntity
{
    public int PerformanceID { get; set; }
    public Performance Performance { get; set; }

    public int SurchargeId { get; set; }
    public Surcharges Surcharge { get; set; }
}

And here is the second class:这是第二类:

public class Surcharges : FullAuditedEntity
{
    public  string Name { get; set; }

    public ICollection<PerformanceSurchargeGeneralSettings> PerformanceSurchargeGeneralSettings{ get; set; }
}

Everything works fine, I can add migration, update my database but if I go to table and check for foreign key reference, I can't see primary key table.一切正常,我可以添加迁移,更新我的数据库,但是如果我去表并检查外键引用,我看不到主键表。 See this screenshot here:在此处查看此屏幕截图:

在此处输入图片说明

I am not able to find newly added table that is Surcharge in the dropdown of primary key table.我无法在主键表的下拉列表中找到新添加的Surcharge表。

And if I execute SP_help for this table, I can find all foreign keys, see the screenshot:如果我对这个表执行SP_help ,我可以找到所有的外键,看截图:

在此处输入图片说明

I don't understand where exactly the issue is ...我不明白问题到底出在哪里......

H, H,

Change your classes as below;更改您的课程如下;

[Table("PerformanceSurchargeGeneralSettings")]
public class PerformanceSurchargeGeneralSetting : FullAuditedEntity
{
    [ForeignKey("PerformanceId")]
    public virtual Performance Performance { get; set; }
    public int PerformanceId { get; set; }

    [ForeignKey("SurchargeId")]
    public virtual Surcharge Surcharge { get; set; }
    public int SurchargeId { get; set; }    
}

[Table("Surcharges")]
public class Surcharge : FullAuditedEntity
{
    [MaxLength(128)]
    public string Name { get; set; }

    public virtual ICollection<PerformanceSurchargeGeneralSetting> PerformanceSurchargeGeneralSettings{ get; set; }
}

My advises我的建议

  • Use singular names for classes.对类使用单数名称。 Surcharges => Surcharge.附加费 => 附加费。
  • Give MaxLength to string properties.MaxLength赋予字符串属性。
  • Use the same case for all Id fields.对所有 Id 字段使用相同的大小写。 PerformanceID => PerformanceId.性能 ID => 性能 ID。 (like you did for SurchageId). (就像您为 SurchageId 所做的那样)。
  • Add virtual key when you want to load data with lazy loading.当你想通过延迟加载加载数据时添加虚拟键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM