我收到了错误消息。 我有2张桌子DonHang,Kho。 DonHang链接到Kho(DonHang.kid - > Kho.id) 我首先使用Visual Studio 2017中的数据库创建了模型DonHang,Kho,类ShopDbContext。 我在我的项目中创建了控制器Home有动作Create(使用向表DonHang添加数据)。但是,当我使用此操作时,网站会自动将数据添加到表格Kho并为tabe DonHang分配新ID,尽管在项目中没有动作创建Kho。
我将项目代码发送给所有人 谢谢!
模特DonHang
[Table("DonHang")]
public partial class DonHang
{
public int id { get; set; }
public int? uid { get; set; }
public int? kid { get; set; }
public int? soluong { get; set; }
public DateTime? ngayxuat { get; set; }
public int? khachhang { get; set; }
public int? thanhtoan { get; set; }
public int? no { get; set; }
public string ghichu { get; set; }
public int? thanhtien { get; set; }
public virtual KhachHang KhachHang1 { get; set; }
public virtual Kho Kho { get; set; }
public virtual TaiKhoan TaiKhoan { get; set; }
}
模特Kho
[Table("Kho")]
public partial class Kho
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Kho()
{
DonHangs = new HashSet<DonHang>();
}
public int id { get; set; }
public int? uid { get; set; }
public int? soluong { get; set; }
public int? gianhap { get; set; }
public int? giaxuat { get; set; }
public DateTime? ngaynhap { get; set; }
[StringLength(250)]
public string tensanpham { get; set; }
public string mota { get; set; }
public int? phanloai { get; set; }
public int? tinhtrang { get; set; }
[StringLength(250)]
public string donvi { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DonHang> DonHangs { get; set; }
public virtual TaiKhoan TaiKhoan { get; set; }
}
ShopDbContex
public partial class ShopDbContext : DbContext
{
public ShopDbContext()
: base("name=ShopDbContext")
{
}
public virtual DbSet<DonHang> DonHangs { get; set; }
public virtual DbSet<KhachHang> KhachHangs { get; set; }
public virtual DbSet<Kho> Khoes { get; set; }
public virtual DbSet<TaiKhoan> TaiKhoans { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<KhachHang>()
.HasMany(e => e.DonHangs)
.WithOptional(e => e.KhachHang1)
.HasForeignKey(e => e.khachhang);
modelBuilder.Entity<Kho>()
.HasMany(e => e.DonHangs)
.WithOptional(e => e.Kho)
.HasForeignKey(e => e.kid);
modelBuilder.Entity<TaiKhoan>()
.HasMany(e => e.DonHangs)
.WithOptional(e => e.TaiKhoan)
.HasForeignKey(e => e.uid);
modelBuilder.Entity<TaiKhoan>()
.HasMany(e => e.KhachHangs)
.WithOptional(e => e.TaiKhoan)
.HasForeignKey(e => e.uid);
modelBuilder.Entity<TaiKhoan>()
.HasMany(e => e.Khoes)
.WithOptional(e => e.TaiKhoan)
.HasForeignKey(e => e.uid);
}
}
控制器
[HttpPost]
public ActionResult Create(DonHang dh)
{
if (ModelState.IsValid)
{
try
{
dh.uid = Convert.ToInt32(Session["UID"]);
dh.khachhang = 3;
dh.ghichu = "";
dh.kid = 29;
dh.ngayxuat = DateTime.Now;
db.DonHangs.Add(dh);
db.SaveChanges();
TempData["Alert"] = "<div class='alert alert-success'>Thêm khách hàng thành công.</div>";
return RedirectToAction("Index");
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
return View();
}
查看 View
答案 0 :(得分:1)
根据您的帖子,如下:
[Table("DonHang")]
public partial class DonHang
{
...
public int? kid { get; set; }
...
public virtual Kho Kho { get; set; }
}
DonHang可以有0或1 Kho。换句话说,DonHang可以在没有Kho的情况下存在。因此,在您的控制器“创建操作”中,您可以注释掉或删除以下行,并查看是否仍会在数据库中创建Kho:
[HttpPost]
public ActionResult Create(DonHang dh)
{
if (ModelState.IsValid)
{
try
{
...
//dh.kid = 29; //Comment out or remove this line
...
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
return View();
}
您分配给dh.Kid的除null之外的任何值都必须在Kho表中具有相应的k.Id。如果没有带有Id 29的Kho,则无法将值29分配给dh.Kid。我怀疑当您尝试插入新的DonHang时,EF会自动创建一个ID为29的Kho。这是有效的,因为除了Id PK之外,Kho表的所有字段都可以为空。