我有两个名为Seminari
和Predbiljezbe
的表。
当我在表Predbiljezbe
中创建新数据行时,我希望该行采用表Seminari
的主键并将其插入表Predbiljezbe
中。
我希望我说的很清楚,请注意,由于我是编程新手,非常感谢您。我为我的英语不好而道歉。
这是我的代码:
控制器:
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Predbiljezbe predbiljezbePrijava)
{
if (ModelState.IsValid)
{
context.Predbiljezbes.Add(predbiljezbePrijava);
context.SaveChanges();
return RedirectToAction("Index");
}
return View(predbiljezbePrijava);
}
实体模型:
public partial class SeminariEntities1 : DbContext
{
public SeminariEntities1()
: base("name=SeminariEntities1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Predbiljezbe> Predbiljezbes { get; set; }
public virtual DbSet<Seminari> Seminaris { get; set; }
}
Predbiljezbe模型:
public partial class Predbiljezbe
{
public int IdPredbiljezba { get; set; }
public string Ime { get; set; }
public string Prezime { get; set; }
public string Datum { get; set; }
public string Adresa { get; set; }
public string Email { get; set; }
public string Telefon { get; set; }
public bool Status { get; set; }
public Nullable<int> IdSeminara { get; set; }
public Nullable<int> BrojPolaznika { get; set; }
public virtual Seminari Seminari { get; set; }
}
Seminari模型:
public partial class Seminari
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Seminari()
{
this.Predbiljezbes = new HashSet<Predbiljezbe>();
}
public int IdSeminar { get; set; }
public string Naziv { get; set; }
public string Opis { get; set; }
public System.DateTime Datum { get; set; }
public Nullable<bool> Popunjen { get; set; }
public string Predavač { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Predbiljezbe> Predbiljezbes { get; set; }
}
答案 0 :(得分:0)
我的方法是使用双重方法:
要插入数据库并返回ID
使用上面的ID插入依赖子项
[HttpPost]
public ActionResult Create(Predbiljezbe predbiljezbePrijava)
{
if (ModelState.IsValid)
{
//add your first obj
context.Predbiljezbes.Add(predbiljezbePrijava);
context.SaveChanges();
//after saving in db its Id is automatically reflected in the object that you inserted
if(predbiljezbePrijava.Id != 0){
var seminariobj = new Seminari{ //insert your data }
// defining parentId into child
seminariobj.predbiljezbePrijavaId = predbiljezbePrijava.Id;
//save child obj
context.Seminari.Add(seminariobj);
context.SaveChanges();
}
return RedirectToAction("Index");
}
return View(predbiljezbePrijava);
}
答案 1 :(得分:0)
我尝试了您的建议,但无法将解决方案应用于我的问题,
也许我在设置表格和表格之间的关系时出现了错误。
这是我的SQL查询,如何设置数据库中的表:
Seminari:
CREATE TABLE [dbo].[Seminari] (
[IdSeminar] INT IDENTITY (1, 1) NOT NULL,
[Naziv] NVARCHAR (MAX) NOT NULL,
[Opis] NVARCHAR (MAX) NULL,
[Datum] DATE NOT NULL,
[Popunjen] BIT NOT NULL,
[Predavač] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([IdSeminar] ASC)
);
Predbilježbe:
CREATE TABLE [dbo].[Predbiljezbe] (
[IdPredbiljezba] INT IDENTITY (1, 1) NOT NULL,
[Ime] NVARCHAR (MAX) NOT NULL,
[Prezime] NVARCHAR (MAX) NOT NULL,
[Datum] NVARCHAR (MAX) NOT NULL,
[Adresa] NVARCHAR (MAX) NOT NULL,
[Email] NVARCHAR (MAX) NULL,
[Telefon] VARCHAR (MAX) NULL,
[Status] NVARCHAR (50) NULL,
[BrojPolaznika] INT NULL,
[IdSeminara] INT NULL,
CONSTRAINT [PK_Predbiljezbe] PRIMARY KEY CLUSTERED ([IdPredbiljezba] ASC),
CONSTRAINT [FK_Predbiljezbe_ToTable] FOREIGN KEY ([IdSeminara]) REFERENCES [dbo].[Seminari] ([IdSeminar])
);
再次,我想要的是在“ Predbiljezbe”中创建新行时,[IdSeminara]列采用表“ Seminari”中的主键[IdSeminar]的值!