所以以前我沒有一個合適的建構式,因為命名沖突。這次命名相同,但仍然失敗。
我收到以下錯誤代碼:
沒有為物體型別“客戶”找到合適的建構式。以下建構式具有無法系結到物體型別屬性的引數:無法在“客戶(字串 firstName,字串 lastName,地址地址,字串電子郵件)”中系結“地址”。
通過以下執行:
using (var db = new EshopContext())
{
var test = db.Products
.Where(p => p.Title == customSearchTag)
.ToList(); //Error here
foreach (var item in test)
{
Console.WriteLine(item.Title " for " item.Price);
}
}
地址.cs
public int Id { get; set; }
public string Street { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public Address(string street, string zipcode, string city, string country)
{
Street = street;
Zipcode = zipcode;
City = city;
Country = country;
}
客戶.cs
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public string Email { get; set; }
public Customer(string firstName, string lastName, Address Address, string email)
{
FirstName = firstName;
LastName = lastName;
this.Address = Address;
Email = email;
}
我希望有人能告訴我為什么會發生這個錯誤。因為我不知道為什么它不會系結屬性
uj5u.com熱心網友回復:
您需要添加一個空建構式以避免該錯誤。
public class Company
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public string Email { get; set; }
public Customer(string firstName, string lastName, Address Address, string email)
{
FirstName = firstName;
LastName = lastName;
this.Address = Address;
Email = email;
}
// Add this
public Customer() {}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517079.html
標籤:实体框架
下一篇:物體框架無法添加資料
