我正在使用EF 4.1 Code First设置,这里是实体。
public class Vendor
{
public int VendorId { get; set; }
public string Name { get; set; }
public virtual ICollection<VendorProduct> VendorProducts { get; set; }
}
public class VendorProduct
{
public int VendorProductId { get; set; }
public int ProductId { get; set; }
public int VendorId { get; set; }
public string VendorProductNumber { get; set; }
public int Quantity { get; set; }
public decimal SalesPrice { get; set; }
public Product Product { get; set; }
public Vendor Vendor { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Manufacturer { get; set; }
public string ManufacturerNumber { get; set; }
public string UPC { get; set; }
public decimal SalesPrice { get; set; }
public string Description { get; set; }
public virtual ICollection<VendorProduct> VendorProducts { get; set; }
}
以下是配置
public class VendorConfiguration : EntityTypeConfiguration<Vendor>
{
public VendorConfiguration()
{
Property(p => p.Name).IsOptional().HasMaxLength(128);
}
}
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
//HasKey(p => p.ProductId);
//HasMany(p => p.Images).WithOptional();
Property(p => p.Name).IsOptional().HasMaxLength(128);
Property(p => p.Manufacturer).IsOptional().HasMaxLength(64);
Property(p => p.ManufacturerNumber).IsOptional().HasMaxLength(32);
Property(p => p.UPC).IsOptional().HasMaxLength(32);
Property(p => p.SalesPrice).IsOptional();
}
}
public VendorProductConfiguration()
{
//HasKey(v => v.VendorProductId);
Property(o => o.Quantity).IsRequired();
Property(o => o.SalesPrice).IsRequired();
Property(o => o.VendorId).IsRequired();
Property(o => o.ProductId).IsRequired();
Property(o => o.VendorProductNumber).IsOptional().HasMaxLength(50);
HasRequired(o => o.Product).WithMany(p => p.VendorProducts).HasForeignKey(o => o.ProductId).WillCascadeOnDelete(false);
}
这是DbContext。
public class UbidContext : DbContext
{
public IDbSet<Product> Products { get; set; }
public IDbSet<Vendor> Vendors { get; set; }
public IDbSet<VendorProduct> VendorProducts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Add any configuration or mapping stuff here
modelBuilder.Configurations.Add(new VendorConfiguration());
modelBuilder.Configurations.Add(new VendorProductConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());
}
public void Seed(UbidContext context)
{
//Create our indexes
context.Database.ExecuteSqlCommand("CREATE INDEX IX_Products_Name ON Products (Name)");
context.Database.ExecuteSqlCommand("CREATE INDEX IX_Products_Manufacturer ON Products (Manufacturer)");
context.Database.ExecuteSqlCommand("CREATE INDEX IX_Products_ManufacturerNumber ON Products (ManufacturerNumber)");
context.Database.ExecuteSqlCommand("CREATE INDEX IX_Products_UPC ON Products (UPC)");
//Add vendors to the database
AddVendors(context);
context.SaveChanges();
//Add products to the database
AddProducts(context);
context.SaveChanges();
//Add vendor products to the database
AddVendorProducts(context);
}
private static void AddVendors(UbidContext context)
{
new List<Vendor>
{
new Vendor()
{
Name = "TestVendor1",
},
new Vendor()
{
Name = "TestVendor2",
},
new Vendor()
{
Name = "TestVendor3",
}
}.ForEach(v => context.Vendors.Add(v));
}
private static void AddProducts(UbidContext context)
{
Image[] images = new Image[1];
images[0] = new Image
{
Url = "http://content.etilize.com/Thumbnail/10006997.jpg"
};
new List<Product>
{
new Product()
{
Manufacturer = "StarTech.com",
ManufacturerNumber = "SV211K",
Name = "StarTech.com SV211K KVM Switch - 2 x 1 - 2 x HD-15 Video",
UPC = "SV211K",
Images = images
},
new Product()
{
Manufacturer = "Targus Group International",
ManufacturerNumber = "CBT300",
Name = "Targus BlackTop Standard Notebook Case - Clamshell - Carrying Strap - 5 Pocket - Nylon - Black, Blue",
UPC = "CBT300"
},
new Product()
{
Manufacturer = "Lenovo Group Limited",
ManufacturerNumber = "31P8700",
Name = "Lenovo Optical ScrollPoint Pro Mouse - Optical - USB, PS/2",
UPC = "31P8700"
},
new Product()
{
Manufacturer = "Epson Corporation",
ManufacturerNumber = "C823071",
Name = "Epson Serial Interface Board with 32K Buffer - 1 x RS-232 Serial",
UPC = "C823071"
},
new Product()
{
Manufacturer = "Cisco Systems, Inc",
ManufacturerNumber = "WSX4013",
Name = "Cisco Catalyst 4000 Series Supervisor Engine II-Plus - 2 x GBIC, 1 x - Supervisor Engine",
UPC = "WSX4013"
}
}.ForEach(p => context.Products.Add(p));
}
private static void AddVendorProducts(UbidContext context)
{
Random random = new Random();
var vps = new List<VendorProduct>()
{
new VendorProduct()
{
ProductId = 1,
VendorId = 1,
Quantity = random.Next(3, 40),
SalesPrice = Converter.ConvertObjToDecimal(random.Next(20, 400)),
},
new VendorProduct()
{
ProductId = 2,
VendorId = 1,
Quantity = random.Next(3, 40),
SalesPrice = Converter.ConvertObjToDecimal(random.Next(20, 400)),
},
new VendorProduct()
{
ProductId = 3,
VendorId = 1,
Quantity = random.Next(3, 40),
SalesPrice = Converter.ConvertObjToDecimal(random.Next(20, 400)),
},
new VendorProduct()
{
ProductId = 4,
VendorId = 2,
Quantity = random.Next(3, 40),
SalesPrice = Converter.ConvertObjToDecimal(random.Next(20, 400)),
},
new VendorProduct()
{
ProductId = 4,
VendorId = 3,
Quantity = random.Next(3, 40),
SalesPrice = Converter.ConvertObjToDecimal(random.Next(20, 400)),
}
};
foreach (var vp in vps)
context.VendorProducts.Add(vp);
}
public class DropCreateIfChangeInitializer : DropCreateDatabaseIfModelChanges<UbidContext>
{
protected override void Seed(UbidContext context)
{
context.Seed(context);
base.Seed(context);
}
}
static UbidContext()
{
Database.SetInitializer<UbidContext>(new DropCreateIfChangeInitializer());
}
}
现在,当它到达VendorProducts时,第一个添加得很好,但第二个不会保存,因为看起来EF没有设置Vendor对象,我看到的模式是对于在同一上下文中的前一个实体上使用vendorid和/或productid时添加的每个vendorproduct,它不会填充供应商或产品,因此它将其设置为null。从我的代码中可以看出,我在下面显式设置VendorId,但是当EF将数据发送到db时,VendorId为null。如果您需要更多信息,请告诉我。
由于
答案 0 :(得分:0)
我已将您的代码复制并粘贴到使用EF 4.1的控制台应用程序中。当我运行它时,我在数据库(SQL Server 2008 R2 Express)中得到了这个结果:
我只删除了图片中的内容和Converter.ConvertObjToDecimal
(没编译,我直接使用random.Next
)。
我认为这是您对代码的期望。你得到另一个结果吗?