Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EF Core多对多联接表,额外属性不起作用_Entity Framework_Entity Framework Core_.net 5_Asp.net5 - Fatal编程技术网

Entity framework EF Core多对多联接表,额外属性不起作用

Entity framework EF Core多对多联接表,额外属性不起作用,entity-framework,entity-framework-core,.net-5,asp.net5,Entity Framework,Entity Framework Core,.net 5,Asp.net5,我希望此联接表具有2个外键,但也添加了一些其他属性: [可序列化] 公共级食品杂货店 { [键,列(顺序=0),必填] public int GroceryItemId{get;set;} [键,列(顺序=1),必填] public int GroceryStoreId{get;set;} 公共虚拟杂货店项目杂货店项目{get;set;} 公共虚拟杂货店杂货店{get;set;} [必需] public int NotInEstablishmentCount{get;set;} [必需] pub

我希望此联接表具有2个外键,但也添加了一些其他属性:

[可序列化]
公共级食品杂货店
{
[键,列(顺序=0),必填]
public int GroceryItemId{get;set;}
[键,列(顺序=1),必填]
public int GroceryStoreId{get;set;}
公共虚拟杂货店项目杂货店项目{get;set;}
公共虚拟杂货店杂货店{get;set;}
[必需]
public int NotInEstablishmentCount{get;set;}
[必需]
public int InEstablishmentCount{get;set;}
[必需]
公共双价{get;set;}
}
将这些实体连接在一起:

public class GroceryItem:VeganItem
{
公共食品杂货项目(字符串名称、字符串品牌、字符串描述、字符串图像)
{
this.Name=Name;
这个。品牌=品牌;
这个。描述=描述;
这个。图像=图像;
}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串品牌{get;set;}
[必需]
公共字符串说明{get;set;}
公共字符串图像{get;set;}
公共虚拟ICollection GroceryItemGroceryStores{get;set;}
}
[可序列化]
公共抽象类VeganItem
{
公共int Id{get;set;}
[必需]
public int IsNotVeganCount{get;set;}
[必需]
public int IsVeganCount{get;set;}
[必需]
公共整数排序查询{get;set;}
[必需]
公共整数评级{get;set;}
[必需]
公共列表标记{get;set;}
//[必需]
公共列表机构{get;set;}
[必需]
public int CurrentRevisionId{get;set;}
}
这些实体:

[可序列化]
公共级杂货店:建立
{
公共杂货店(字符串名称、字符串位置ID、字符串街道、字符串郊区、字符串城市、字符串街道编号)
{
this.Name=Name;
this.PlaceId=PlaceId;
这条街=这条街;
this.StreetNumber=街道编号;
这个。城市=城市;
这个。郊区=郊区;
}
}
[可序列化]
公共抽象类的建立
{
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串PlaceId{get;set;}
[必需]
公共字符串Street{get;set;}
公共字符串{get;set;}
公共字符串City{get;set;}
公共列表纯素{get;set;}
公共字符串StreetNumber{get;set;}
}
因此,我尝试将我的联接表指定为在数据库上下文中定义多对多关系时要使用的表:


builder.Entity()
.有许多(p=>p.机构)
.有许多(p=>p.素食主义者)
.UsingEntity(j=>j.ToTable(“GroceryItemGroceryStores”);
有些地方不太正确,因为我在运行
dotnet ef migrations add InitialCreate
时遇到此错误:

无法对实体类型使用表'GroceryItemGroceryStores' “GroceryItemGroceryStore”,因为它正用于实体类型 “GroceryItemGroceryStore(字典)”和 其他实体类型,但没有链接关系。添加 主键属性上“GroceryItemGroceryStore”的外键 并指向映射到的另一个实体上的主键 “杂货店杂货店”

我哪里做错了

编辑-插入数据时出现问题
所有表都已成功创建。但是,
GroceryItemGroceryStores
table和
GroceryStores
table在插入一个在其上建立的
GroceryItem
后,不会得到任何插入

以下是发送用于添加
GroceryItem
的有效负载,该有效负载还应添加
GroceryStore
GroceryItemGroceryStore

{
    "name": "yummy fofod",
    "brand": "goodfoobdgffe",
    "description": "dfsad",
    "establishments": [{
        "name": "Macdonalds",
        "street": "Beach Rd",
        "placeId": "fsadfsdfsdfsdfsadfsadfsdf"
    }],
    "tags": [
        {
            "name": "sdf",
            "id": "9",
            "iconCodePoint": 23145
        }
    ]
}
这是处理上述有效负载请求的控制器:

[HttpPost]
public async Task<ActionResult<GroceryItem>> PostGroceryItem(GroceryItem groceryItem)
{
    _context.GroceryItems.Add(groceryItem);
    await _context.SaveChangesAsync();

    return CreatedAtAction("GetGroceryItem", new { id = groceryItem.Id }, groceryItem);
}
[HttpPost]
公共异步任务PostGroceryItem(GroceryItem GroceryItem)
{
_context.GroceryItems.Add(groceryItem);
wait_context.SaveChangesAsync();
返回CreateDataAction(“GetGroceryItem”,新的{id=groceryItem.id},groceryItem);
}

以下两个属性的存在-

public-virtualgroceryitem-GroceryItem{get;set;}
公共虚拟杂货店杂货店{get;set;}

GroceryItemGroceryStore
模型中,已经通过连接实体
GroceryItemGroceryStore
GroceryItem
GroceryStore
置于多对多关系中

然后,关系配置代码尝试在它们之间创建新的多对多关系,但尝试使用相同的连接实体。这就是问题所在,这正是错误消息所说的

删除配置代码,它将自动工作

如果要手动配置关系,请将其更改为-

builder.Entity()
.HasOne(p=>p.GroceryItem)
.有许多(p=>p.GroceryItemGroceryStores)
.HasForeignKey(p=>p.GroceryItemId);
builder.Entity()
.HasOne(p=>p.GroceryStore)
.有很多
.HasForeignKey(p=>p.GroceryStoreId);
或者,最好添加以下属性-

public虚拟ICollection GroceryItemGroceryStores{get;set;}
转到
GroceryStore
模型,并将配置更改为-

builder.Entity()
.HasOne(p=>p.GroceryItem)
.有许多(p=>p.GroceryItemGroceryStores)
.HasForeignKey(p=>p.GroceryItemId);
builder.Entity()
.HasOne(p=>p.GroceryStore)
.有许多(p=>p.GroceryItemGroceryStores)
.HasForeignKey(p=>p.GroceryStoreId);
编辑:
您应该从
VeganItem
模型中删除
机构
属性,并从
Esta中删除
VeganItems
属性