嗨,我正在嘗試將顏色顏色的屬性添加到模型中,這是一個字串陣列。
模型
public class dog
{
// other properties
public ICollection<string> Colors { get; set; }
}
資料庫背景關系
public DataContext(DbContextOptions options) : base(options)
{
}
public DbSet<Dog> Dogs { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<AppUser>().HasMany(user => user.Dogs).WithOne(dog=> dog.AppUser)
.OnDelete(DeleteBehavior.Cascade);
}
如果我想要洗掉一條狗,那么顏色也會被洗掉,我該怎么辦?因為我無法運行遷移,因為我收到此錯誤:
無法映射屬性“dogs.Colors”,因為它屬于“Collection”型別,它不是受支持的原始型別或有效的物體型別。顯式映射此屬性,或使用“[NotMapped]”屬性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略它
我嘗試搜索這個問題,但我并沒有真正找到對我的問題有用的答案。
任何人都可以向我解釋是什么原因造成的,我應該怎么做才能解決它?
uj5u.com熱心網友回復:
你不能在你的資料庫模型中使用 ICollection 因為它在資料庫中沒有等效的型別(例如 sqlsever , mysql ,...)
實際上,您應該以這種方式為“顏色”創建其他表:
public class dog
{
public int Id {get; set;}
// other properties
public ICollection<Color> Colors { get; set; }
}
public class Color
{
public string Value {get; set;}
public int DogId {get; set}
public dog Dog{ get; set; }
}
你也可以在你的 DbContext 類中添加:
public DbSet<Color> Colors { get; set; }
此外,如果您不想將 ICollection 保存在資料庫中,您可以使用“[NotMapped]”屬性或在“OnModelCreating”中使用“EntityTypeBuilder.Ignore”來忽略它
例如 :
[NotMapped]
public ICollection<string> Colors { get; set; }
uj5u.com熱心網友回復:
我正在考慮兩種處理方法...
- 使用支持欄位如下:
支持欄位允許 EF 讀取和/或寫入欄位而不是屬性。當使用類中的封裝來限制應用程式代碼對資料訪問的使用和/或增強語意時,這可能很有用,但應在不使用這些限制的情況下從資料庫讀取和/或寫入值/增強
public class Dog
{
// other properties
private string _colors; //this is the backing field
public ICollection<string> Colors
{
get
{
if (string.IsNullOrWhiteSpace(_colors))
return new List<string>(); //empty list
return _colors.Split(",");
}
set
{
//you can have other logic, instead of setting it directly
//in this case it will overwrite the old value
_colors = string.Join(",", value);
}
}
}
在您的資料庫狗表,有列名的顏色與NVARCHAR或VARCHAR資料型別。
當您存盤顏色值(字串值串列)時,將其轉換為串聯的單個字串值。同樣,每當您希望將代碼中的單個字串值作為集合時,請使用字串到陣列的轉換。
重新建模你的狗物體,
這將使您在資料庫上再創建兩個表。這些表是Colors和DogsColors。這里要注意的一件事是 Dog 和 Color 都有多對多的關系。
根據您的 dotnet 核心版本,您可能需要創建一個或多個物體模型。
如果您使用的是Dotnet Core 5或更高版本,則只需要一個物體模型Color。否則,您可能還必須添加DogsColor物體模型。
Dotnet 核心 5 或更高版本:
.net core 5多對多關系
多對多關系需要雙方的集合導航屬性。它們會像其他型別的關系一樣被約定發現。
public class Color {
public int Id { get; set; }
public string Color { get; set;}
public ICollection<Dog> Dogs {get;set;} //for many to many relationship
}
public class Dog
{
public ICollection<Color> Colors { get; set; }
}
//dbcontext configuration
builder.Entity<Dog>()
.HasMany(i => i.Colors)
.WithMany(i => i.Dogs)
.UsingEntity<Dictionary<string, object>>(
"DogColors",
x => x.HasOne<Color>().WithMany().HasForeignKey("ColorId"),
x => x.HasOne<Dog>().WithMany().HasForeignKey("DogId"));
如果您使用的是Dotnet 核心 3或更低版本:
public class Color {
public int Id { get; set; }
public string Color { get; set;}
}
public class DogColor {
public int Id { get; set; }
public Dog Dog { get; set;}
public Color Color { get; set;}
}
public class Dog
{
public ICollection<DogColor> DogColors { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/373985.html
標籤:asp.net核心
上一篇:Asp.NetCore-在textarea中顯示字串串列值?
下一篇:.net核心請求串列引數總是為空
