C# | Entity Framework |无法从固定大小的 Array 类型中删除项目
C# | Entity Framework | An item cannot be removed from a fixed size Array of type
在 EF 中,我尝试使用以下语法添加记录:
Context.Set<TABLENAME>().Add(TABLEROW)
我收到这个疯狂的错误:
"An item cannot be removed from a fixed size Array of type"
我做了一些目击,它建议我从生成的上下文文件从 DBSet 更改为 List:
public partial class GeniusDBContext : DbContext
{
public virtual DbSet<TABLENAME> TABLENAMES { get; set; }
...
}
到
public partial class GeniusDBContext : DbContext
{
public virtual List<TABLENAME> TABLENAMES { get; set; }
...
}
这很荒谬,因为如果您更改 .edmx 文件,您将丢失这些更改。还有其他解决方法吗?
DbSet<>
是正确的,没必要改成List<>
。我认为找到的建议具有误导性,并不是问题所在。
要添加新实体,试试这个:
var ctx = new GeniusDBContext();
var entity = new MyEntity();
ctx.MyEntities.Add(entity);
我使用了 John 的方法,但它不适用于 EF6。
所以我这样骗人:
List<MyEntitie> lstEntity= new List<MyEntitie>();
lstEntity.Add(new MyEntitie
{
FacID = FacID,
FoodID = order.FoodID,
OrderID = IsOrder.ID,
Quantity = 1,
ToppingID = -1,
});
db.MyEntities.AddRange(lstEntity);
终于,它成功了!希望对大家有所帮助
在 EF 中,我尝试使用以下语法添加记录:
Context.Set<TABLENAME>().Add(TABLEROW)
我收到这个疯狂的错误: "An item cannot be removed from a fixed size Array of type"
我做了一些目击,它建议我从生成的上下文文件从 DBSet 更改为 List:
public partial class GeniusDBContext : DbContext
{
public virtual DbSet<TABLENAME> TABLENAMES { get; set; }
...
}
到
public partial class GeniusDBContext : DbContext
{
public virtual List<TABLENAME> TABLENAMES { get; set; }
...
}
这很荒谬,因为如果您更改 .edmx 文件,您将丢失这些更改。还有其他解决方法吗?
DbSet<>
是正确的,没必要改成List<>
。我认为找到的建议具有误导性,并不是问题所在。
要添加新实体,试试这个:
var ctx = new GeniusDBContext();
var entity = new MyEntity();
ctx.MyEntities.Add(entity);
我使用了 John 的方法,但它不适用于 EF6。 所以我这样骗人:
List<MyEntitie> lstEntity= new List<MyEntitie>();
lstEntity.Add(new MyEntitie
{
FacID = FacID,
FoodID = order.FoodID,
OrderID = IsOrder.ID,
Quantity = 1,
ToppingID = -1,
});
db.MyEntities.AddRange(lstEntity);
终于,它成功了!希望对大家有所帮助