Asp.net Enity框架外键
我有三个优点Asp.net Enity框架外键,asp.net,asp.net-mvc,entity-framework,Asp.net,Asp.net Mvc,Entity Framework,我有三个优点 public class Customer { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] public int Customerid { get; set; } public string Name { get; set; } public string email { get; set; } public string Phoneno { get; se
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Customerid { get; set; }
public string Name { get; set; }
public string email { get; set; }
public string Phoneno { get; set; }
}
第二种美德
public class Merchant
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Merchantid { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string MerchantName { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string BusinessName { get; set; }
[Required]
public string OfficePhno { get; set; }
}
public class Transaction
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TransactionId { get; set; }
public int Merchantid {get; set;}
public int Customerid {get; set;}
public int Amount { get; set; }
public Customer customer {get; set;}
public Merchant merchant {get; set;}
}
在上面的交易表中有两个ID商户ID和客户ID,我希望这是客户和商户表的外键,
请向我解释如何添加外键约束,我通过了许多示例,但没有得到这个问题的答案
另一个疑问是,若我在事务表中添加客户类型,我是否能够在实体的事务表中获取客户的详细信息
框架
我试图通过以下代码添加约束
[ForeignKey("Customerid")]
pubic virtual Customer customer {get; set;}
我得到一个例外如下
其他信息:属性“Customer”上的ForeignKeyAttribute
“Dutch.Models.Transaction”类型上的“”无效。外键名
在依赖类型上找不到“Customerid”
“Dutch.Models.Transaction”。名称值应该是逗号分隔的
外键属性名称的列表
我终于在外键的约束下做到了 我的模型现在看起来像这样
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Customerid { get; set; }
public string Name { get; set; }
public string email { get; set; }
public string Phoneno { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
public class Merchant
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Merchantid { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string MerchantName { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string BusinessName { get; set; }
[Required]
public string OfficePhno { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
我终于在外键的约束下做到了 我的模型现在看起来像这样
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Customerid { get; set; }
public string Name { get; set; }
public string email { get; set; }
public string Phoneno { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
public class Merchant
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Merchantid { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string MerchantName { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string BusinessName { get; set; }
[Required]
public string OfficePhno { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
在EF6中,据我所知,1-ON-1关系将使用约定PK=FK,因此您有FK约束,但这些将考虑ID。如果你在交易中添加客户类型,只要你懒洋洋地/显式地加载它们,你就可以获得详细信息。你能解释一下吗?我还是不明白这可能会有帮助:我还有一个疑问,如果我使用ef获取交易记录,会不会获取客户的所有列?据我所知,在EF6中,1-ON-1关系将使用约定PK=FK,因此您有FK约束,但这些将考虑ID。如果您在交易中添加客户类型,您将能够获得详细信息,只要您懒洋洋地/明确地加载它们。您能解释一下我仍然不明白这可能会有帮助:我还有一个疑问,如果我使用ef获取交易记录,是否会获取客户的所有列?