entity-framework - 设置包含集合的实体框架对象
问题描述
我的Customer
班级包含我班级的集合Card
。我不确定如何表达与实体框架的这种关系。
我收到以下错误:
没有为实体类型“卡片”找到合适的构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法绑定“id”、“cardBrand”、“last4”、“expMonth”、“expYear”、“cardholderName”、“billingAddress”、“fingerprint” , 'cardType', 'prepaidType', 'bin' in 'Card(string id, string cardBrand, string last4, Nullable expMonth, Nullable expYear, string cardholderName, Address billingAddress, string指纹, string cardType, string prepaidType, string bin)' .
客户等级:
public class Customer
{
public Customer(string id = null, string createdAt = null, string updatedAt = null, IList<Card> cards = null, string givenName = null, string familyName = null, string nickname = null, string companyName = null, string emailAddress = null, Address address = null, string phoneNumber = null, string birthday = null, string referenceId = null, string note = null, CustomerPreferences preferences = null, IList<CustomerGroupInfo> groups = null, string creationSource = null, IList<string> groupIds = null, IList<string> segmentIds = null);
[JsonProperty("group_ids", NullValueHandling = NullValueHandling.Ignore)]
public IList<string> GroupIds { get; }
[JsonProperty("creation_source", NullValueHandling = NullValueHandling.Ignore)]
public string CreationSource { get; }
[JsonProperty("groups", NullValueHandling = NullValueHandling.Ignore)]
public IList<CustomerGroupInfo> Groups { get; }
[JsonProperty("preferences", NullValueHandling = NullValueHandling.Ignore)]
public CustomerPreferences Preferences { get; }
[JsonProperty("note", NullValueHandling = NullValueHandling.Ignore)]
public string Note { get; }
[JsonProperty("reference_id", NullValueHandling = NullValueHandling.Ignore)]
public string ReferenceId { get; }
[JsonProperty("birthday", NullValueHandling = NullValueHandling.Ignore)]
public string Birthday { get; }
[JsonProperty("phone_number", NullValueHandling = NullValueHandling.Ignore)]
public string PhoneNumber { get; }
[JsonProperty("address", NullValueHandling = NullValueHandling.Ignore)]
public Address Address { get; }
[JsonProperty("email_address", NullValueHandling = NullValueHandling.Ignore)]
public string EmailAddress { get; }
[JsonProperty("company_name", NullValueHandling = NullValueHandling.Ignore)]
public string CompanyName { get; }
[JsonProperty("nickname", NullValueHandling = NullValueHandling.Ignore)]
public string Nickname { get; }
[JsonProperty("family_name", NullValueHandling = NullValueHandling.Ignore)]
public string FamilyName { get; }
[JsonProperty("given_name", NullValueHandling = NullValueHandling.Ignore)]
public string GivenName { get; }
[JsonProperty("cards", NullValueHandling = NullValueHandling.Ignore)]
public IList<Card> Cards { get; }
[JsonProperty("updated_at", NullValueHandling = NullValueHandling.Ignore)]
public string UpdatedAt { get; }
[JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
public string CreatedAt { get; }
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public string Id { get; }
[JsonProperty("segment_ids", NullValueHandling = NullValueHandling.Ignore)]
public IList<string> SegmentIds { get; }
public override bool Equals(object obj);
public override int GetHashCode();
public Builder ToBuilder();
public override string ToString();
protected void ToString(List<string> toStringOutput);
public class Builder
{
public Builder();
public Builder Address(Address address);
public Builder Birthday(string birthday);
public Customer Build();
public Builder Cards(IList<Card> cards);
public Builder CompanyName(string companyName);
public Builder CreatedAt(string createdAt);
public Builder CreationSource(string creationSource);
public Builder EmailAddress(string emailAddress);
public Builder FamilyName(string familyName);
public Builder GivenName(string givenName);
public Builder GroupIds(IList<string> groupIds);
public Builder Groups(IList<CustomerGroupInfo> groups);
public Builder Id(string id);
public Builder Nickname(string nickname);
public Builder Note(string note);
public Builder PhoneNumber(string phoneNumber);
public Builder Preferences(CustomerPreferences preferences);
public Builder ReferenceId(string referenceId);
public Builder SegmentIds(IList<string> segmentIds);
public Builder UpdatedAt(string updatedAt);
}
}
我的卡类:
public class Card
{
public Card(string id = null, string cardBrand = null, string last4 = null, long? expMonth = null, long? expYear = null, string cardholderName = null, Address billingAddress = null, string fingerprint = null, string cardType = null, string prepaidType = null, string bin = null);
[JsonProperty("prepaid_type", NullValueHandling = NullValueHandling.Ignore)]
public string PrepaidType { get; }
[JsonProperty("card_type", NullValueHandling = NullValueHandling.Ignore)]
public string CardType { get; }
[JsonProperty("fingerprint", NullValueHandling = NullValueHandling.Ignore)]
public string Fingerprint { get; }
[JsonProperty("billing_address", NullValueHandling = NullValueHandling.Ignore)]
public Address BillingAddress { get; }
[JsonProperty("cardholder_name", NullValueHandling = NullValueHandling.Ignore)]
public string CardholderName { get; }
[JsonProperty("exp_year", NullValueHandling = NullValueHandling.Ignore)]
public long? ExpYear { get; }
[JsonProperty("exp_month", NullValueHandling = NullValueHandling.Ignore)]
public long? ExpMonth { get; }
[JsonProperty("last_4", NullValueHandling = NullValueHandling.Ignore)]
public string Last4 { get; }
[JsonProperty("card_brand", NullValueHandling = NullValueHandling.Ignore)]
public string CardBrand { get; }
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public string Id { get; }
[JsonProperty("bin", NullValueHandling = NullValueHandling.Ignore)]
public string Bin { get; }
public override bool Equals(object obj);
public override int GetHashCode();
public Builder ToBuilder();
public override string ToString();
protected void ToString(List<string> toStringOutput);
public class Builder
{
public Builder();
public Builder BillingAddress(Address billingAddress);
public Builder Bin(string bin);
public Card Build();
public Builder CardBrand(string cardBrand);
public Builder CardholderName(string cardholderName);
public Builder CardType(string cardType);
public Builder ExpMonth(long? expMonth);
public Builder ExpYear(long? expYear);
public Builder Fingerprint(string fingerprint);
public Builder Id(string id);
public Builder Last4(string last4);
public Builder PrepaidType(string prepaidType);
}
}
解决方案
实体框架类:仅限 POCO!
问题是您的类型没有默认构造函数,就像错误所说的那样。
使用实体框架时,类代表您的表以及表之间的关系。它们应该是简单的 POCO(普通的旧 CRL 对象)。所以:
- 没有构造函数
- 没有字段
- 仅获取/设置属性
- 最好没有方法,毕竟:类代表你的表,表没有任何方法
实体框架中的一对多
Customers
所以你想在and之间创建一个一对多的关系Cards
:everyCustomer
有零个或多个Cards
,everyCard
完全属于 one Customer
。
在实体框架中,您将使用以下类对此进行建模:
public class Customer
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Customer has zero or more Cards (one-to-many)
public virtual ICollection<Card> Cards {get; set;}
}
public class Card
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Card is a Card of exactly one Customer, using foreign key:
public int CustomerId {get; set;}
public virtual Customer Customer {get; set;}
}
当然还有 DbContext:
public class CustomerDbContext : DbContext
{
public DbSet<Customer> Customers {get; set;}
public DbSet<Card> Cards {get; set;}
}
因为我遵循了实体框架的约定,尤其是关于复数和虚拟 ICollections 的约定,所以实体框架会自动检测表、表中的列和一对多关系。
在实体框架中,表的列由非虚拟属性表示。虚拟属性表示表之间的关系(一对多、多对多)。
外键 CustomerId 是 Cards 表中的实列,因此它不是虚拟 Cards。Customer 不是列,它表示 Card 和 Customer 之间的关系,因此它是虚拟的
顺便说一句:我把它做成了一个virtual ICollection<Card>
,而不是某种列表,因为Card[4]
在获取数据后不会有定义的含义,而像 Add / Remove 这样的 ICollection 方法确实有定义的含义。
只有当我想偏离约定时:不同的表名,或者列的不同标识符,或者它们的类型,才需要一些 fluent API 的属性。
不过,如果您想将一对多关系指定为 fluent API DbContext.OnModelCreating
,请使用以下命令:
// Every Customer has zero or more Cards, using foreign key (one-to-many)
modelBuilder.Entity<Customer>().HasMany(customer => customer.Cards)
.WithRequired(card => card.Customer)
.HasForeignKey(card => card.CustomerId);
换句话说:客户表中的每个客户都有零个或多个卡,通过属性 Customer.Cards。每张卡通过属性 Card.Customer 完全属于一个客户。在数据库中,这是使用外键 Card.CustomerId 强制执行的。
推荐阅读
- ansible - Ansible 调试 INI 格式
- python - 如何理解联合图中的 p 值?
- python-3.x - Drf令牌认证密码重置
- alexa - 由于无效正文 ASK CLI 导致解析错误
- c# - C# - 从另一个方法停止计时器
- sql-server - 关于运行总计的 tsql 帮助
- null - 无法确定 R 数据框中的空白、空值、na 或其他任何类型
- reactjs - 通过 refs 访问 phone-react-input-2 组件时,我没有得到更新的状态
- office-js - Outlook 插件 - 需要获取打开/读取 UI 的电子邮件数据
- c++ - cpp 程序在读取 csv 文件时返回核心转储