如何在表y中插入与表x 0-0/1(x,y)相关的数据

时间:2018-08-21 00:56:27

标签: asp.net-mvc entity-framework

嗨,我正在使用“模型优先”的EF。

我想在表中添加更多为特殊类别指定的详细信息 但是当我添加这些依赖于在产品表中添加数据的详细信息时,就会出现异常。

public static void addproduct(ProductViewModel vm ,string userid)
        {
            var model = Mapper.Map<ProductViewModel, Product>(vm);
            model.CountSell = 0;

            byte[] binaries = new byte[vm.postprofilimgae.ContentLength];
            vm.postprofilimgae.InputStream.Read(binaries, 0, vm.postprofilimgae.ContentLength);
            model.ProfileImage = binaries;
            foreach (var file in vm.postProductimages)
            {
                var filedetails = new ProductImage();
                var data = new byte[file.ContentLength];
                filedetails.ContentLength = file.ContentLength;
                filedetails.FileName = file.FileName;
                file.InputStream.Read(data, 0, file.ContentLength);
                filedetails.ProductImage1 = data;
                filedetails.ProductID= model.ProductID;
                model.ProductImages.Add(filedetails);
            }
            using (var db = new Entities())
            {
                model.ActivationID = db.ActivationStatus.Single(x => x.Name == "Active").ActivationID;
                model.SubCategoryID = vm.SubCategoryID;
                model.EmployeeID= db.Employees.Single(x => x.UserID == userid).EmployeeID;
                model.CountSell = 0;

                VichDetails detailsmodel = new VichDetails();
                if (db.SubCategories.Single(x=>x.SubCategoryID == vm.SubCategoryID).MainCategoryID == db.MainCategories.Single(x=> x.Name == Categories.vehicles).MainCategoryID)
                {
                    VichDetails moredetails = new VichDetails();
                    detailsmodel = Mapper.Map<ProductViewModel, VichDetails>(vm);
                    //detailsmodel.ProductID = model.ProductID;
                    db.VichDetails.Add(detailsmodel);
                }
                db.SaveChanges();
                model.VichDetailsID = detailsmodel.Id;
                db.Products.Add(model);
                db.SaveChanges();
            }
        }

我收到此异常:

1 个答案:

答案 0 :(得分:0)

在您的产品中,您将考虑添加:

public virtual VichDetail VichDetail { get; set; }

,然后代替:

if (db.SubCategories.Single(x=>x.SubCategoryID == vm.SubCategoryID).MainCategoryID == db.MainCategories.Single(x=> x.Name == Categories.vehicles).MainCategoryID)
{
    VichDetails moredetails = new VichDetails();
    detailsmodel = Mapper.Map<ProductViewModel, VichDetails>(vm);
    db.VichDetails.Add(detailsmodel);
}
db.SaveChanges();
model.VichDetailsID = detailsmodel.Id;
db.Products.Add(model);
db.SaveChanges();

它看起来像:

if (db.SubCategories.Single(x=>x.SubCategoryID == vm.SubCategoryID).MainCategoryID == db.MainCategories.Single(x=> x.Name == Categories.vehicles).MainCategoryID)
{
    VichDetails detailsmodel = Mapper.Map<ProductViewModel, VichDetails>(vm);
    model.VichDetails = detailsModel;
}
db.Products.Add(model);
db.SaveChanges();

您遇到的问题是,在保存产品之前在产品上设置新的VichDetail ID时,您仅关联了产品,而未必关联了VichDetail。即 if (db.SubCategories.Single(x=>x.SubCategoryID == vm.SubCategoryID).MainCategoryID == db.MainCategories.Single(x=> x.Name == Categories.vehicles).MainCategoryID)

如果此条件为假,则VichDetail不会添加到DbContext。

在创建VichDetail时,代码看起来很混乱,但是在从视图模型映射原始细节之前,先检查该条件,然后再创建一个看上去不可用的VichDetail“ moredetails”。如果没有通过该条件,您将有一个空的VichDetails(详细信息模型),可能带有将在您的产品上设置的默认ID(0?)。 (型号)

您不能简单地在条件内移动model.VichDetailsID = detailsmodel.id,因为您依赖于设置FK ID,除非将相关实体保存到DbContext,否则FK ID将不可用。导航属性通常比与实体中的PK和FK进行交互要容易得多。让EF进行关联关系的提升。

如果您的实体已经具有这些导航属性,那么您将要使用这些属性,并避免直接设置FK。