我正在做一個必須使用 EF 的專案,但嘗試向表中添加新資料似乎每次都失敗。我不知道為什么/如何解決它。
執行的代碼是:
private static void AddCustomer()
{
WriteAdminMenu("Add new customer");
Console.Write("First name: ");
...
WriteAdminMenu("Adding customer...");
using (var db = new EshopContext())
{
db.Addresses.Add(new Address(customerStreet, customerZip, customerCity, customerCountry));
}
}
我收到以下錯誤:
System.InvalidOperationException:沒有為物體型別“地址”找到合適的建構式。以下建構式具有無法系結到物體型別屬性的引數:無法在“地址(字串街道、字串 zip、字串城市、字串國家)”中系結“zip”
背景關系檔案:
public class EshopContext : DbContext
{
public DbSet<Customer>? Customer { get; set; }
public DbSet<Address>? Addresses { get; set; }
public DbSet<Order>? Orders { get; set; }
public DbSet<Product>? Products { get; set; }
public DbSet<Tag>? Tags { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("Data Source=TotallyMyIp;Initial Catalog=MyDatabaseName;Persist Security Info=True;User ID=TotalyMyUserName;Password=totallySecretPassword");
}
我的地址模型:
public class Address
{
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 zip, string city, string country)
{
Street = street;
Zipcode = zip;
City = city;
Country = country;
}
}
uj5u.com熱心網友回復:
框架沒有辦法知道zip與Zipcode.
保持你的名字一致:
public Address(string street, string zipcode, string city, string country)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517080.html
標籤:C#实体框架
上一篇:物體框架中沒有合適的建構式錯誤
