public class AroundShop : ShopBase
{
public UserInfo User { get; set; }
public virtual ICollection<AroundGoods> GoodsList { get; set; }
}
public class AroundGoods : GoodsBase
{
public AroundShop Shop { get; set; }
}
public class AroundShopfiguration : EntityTypeConfiguration<AroundShop>
{
public AroundShopfiguration()
{
HasMany(d => d.GoodsList).WithRequired(l => l.Shop);
}
}
//我现在这样写就查找不到shop
List<AroundGoods> list = new List<AroundGoods>();
for (int i = 0; i < 50; i++)
{
list.Add(new AroundGoods { GoodsName = "瓜子" + (i + 1), GoodsPrice = 5 + new Random().Next(0, 4), GoodsIntroduce = "good", GoodsImg = imgarr[i > 24 ? i - 25 : i],
Shop = new AroundShop { Id = new Guid("B8A98A53-04FA-E411-BD4C-A9F0B66714A5") } });
}
db.AroundGoods.AddRange(list);
db.SaveChanges();
4 个解决方案
#1
这代码看起来写的很酷,却违背了ef基本使用规则。
如果你要给某个店添加若干商品,正确的操作应该是:
先拿到店子的对象,然后把商品对象添加到店子对象的商品列表里。
一:店子已存在表里
var shop = db.shops.FirstOrDefault(p=>p.id = xxx);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.SaveChanges();
二:创建一个新店子
var shop = new shop(....);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.shops.Add(shop);
db.SaveChanges();
如果你要给某个店添加若干商品,正确的操作应该是:
先拿到店子的对象,然后把商品对象添加到店子对象的商品列表里。
一:店子已存在表里
var shop = db.shops.FirstOrDefault(p=>p.id = xxx);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.SaveChanges();
二:创建一个新店子
var shop = new shop(....);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.shops.Add(shop);
db.SaveChanges();
#2
商品表中的店子id不需要赋值,系统会自动根据外键关系填值
#3
这样的话我倒是明白,但是要多查询一遍,会不会拖节奏啊?
#4
AroundShop asmodel = new AroundShop { Id = new Guid("B8A98A53-04FA-E411-BD4C-A9F0B66714A5") };
List<AroundGoods> list = new List<AroundGoods>();
for (int i = 0; i < 50; i++)
{
list.Add(new AroundGoods { GoodsName = "百事" + (i + 1), GoodsPrice = 5 + new Random().Next(0, 4), GoodsIntroduce = "good", GoodsImg = imgarr[i > 24 ? i - 25 : i] });
}
db.AroundShop.Attach(asmodel);
asmodel.GoodsList = list;
db.SaveChanges();
如此解决。谢谢楼上提醒!
#1
这代码看起来写的很酷,却违背了ef基本使用规则。
如果你要给某个店添加若干商品,正确的操作应该是:
先拿到店子的对象,然后把商品对象添加到店子对象的商品列表里。
一:店子已存在表里
var shop = db.shops.FirstOrDefault(p=>p.id = xxx);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.SaveChanges();
二:创建一个新店子
var shop = new shop(....);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.shops.Add(shop);
db.SaveChanges();
如果你要给某个店添加若干商品,正确的操作应该是:
先拿到店子的对象,然后把商品对象添加到店子对象的商品列表里。
一:店子已存在表里
var shop = db.shops.FirstOrDefault(p=>p.id = xxx);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.SaveChanges();
二:创建一个新店子
var shop = new shop(....);
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
shop.goodsList.Add(new goods(.....));
....
db.shops.Add(shop);
db.SaveChanges();
#2
商品表中的店子id不需要赋值,系统会自动根据外键关系填值
#3
这样的话我倒是明白,但是要多查询一遍,会不会拖节奏啊?
#4
AroundShop asmodel = new AroundShop { Id = new Guid("B8A98A53-04FA-E411-BD4C-A9F0B66714A5") };
List<AroundGoods> list = new List<AroundGoods>();
for (int i = 0; i < 50; i++)
{
list.Add(new AroundGoods { GoodsName = "百事" + (i + 1), GoodsPrice = 5 + new Random().Next(0, 4), GoodsIntroduce = "good", GoodsImg = imgarr[i > 24 ? i - 25 : i] });
}
db.AroundShop.Attach(asmodel);
asmodel.GoodsList = list;
db.SaveChanges();
如此解决。谢谢楼上提醒!