I have created the following two classes:
我创建了以下两个类:
public class QuoteGeneral
{
public int QuoteGeneralID { get; set; }
public QuoteStatus Status { get; set; }
//Code and annotation removed for clarity
public int? QuoteCustomerID { get; set; }
public virtual QuoteCustomer QuoteCustomer { get; set; }
public virtual QuoteProduction QuoteProduction { get; set; }
}
public class QuoteProduction
{
public int QuoteGeneralID { get; set; }
public int testrun { get; set; }
public virtual QuoteGeneral QuoteGeneral { get; set; }
}
And then I added:
然后我补充说:
modelBuilder.Entity<QuoteProduction>()
.HasKey(e => e.QuoteGeneralID);
modelBuilder.Entity<QuoteGeneral>()
.HasOptional(s => s.QuoteProduction)
.WithRequired(ad => ad.QuoteGeneral);
To set the QuoteProduction.QuoteGeneralID
field to be both the primary and foreign key. So, now when the database is generated it will look something like:
将QuoteProduction.QuoteGeneralID字段设置为主键和外键。所以,现在生成数据库时,它看起来像:
I would like a record to be automatically created in the QuoteProduction
table whenever one is entered into the QuoteGeneral
table. Is this possible? Or, do I have to add it myself? If I do have to add it myself, what is the best way of capturing the QuoteGeneralID
value? I don't think SELECT MAX(QuoteGeneralID) FROM QuoteGeneral
will always be reliable since I may have a lot of users working with the database, and it may pull the wrong value.
我希望只要在QuoteGeneral表中输入一个记录就会在QuoteProduction表中自动创建。这可能吗?或者,我必须自己添加吗?如果我必须自己添加它,捕获QuoteGeneralID值的最佳方法是什么?我不认为SELECT MAX(QuoteGeneralID)FROM QuoteGeneral总是可靠的,因为我可能有很多用户使用数据库,它可能会拉错值。
EDIT: What worked for me (for reference to possibly help other in the future)
编辑:什么对我有用(以供将来可能帮助其他人参考)
public ActionResult Create([Bind(Include = "list of bind fields")] QuoteGeneral quoteGeneral)
{
if (ModelState.IsValid)
{
db.QuoteGenerals.Add(quoteGeneral);
var qp = new QuoteProduction
{
QuoteGeneralID = quoteGeneral.QuoteGeneralID
};
db.QuoteProductions.Add(qp);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(quoteGeneral);
}
3 个解决方案
#1
3
As the QuoteGeneralID is a foreign key into the QuoteGeneral table you can simply create the QuoteProduction record:
由于QuoteGeneralID是QuoteGeneral表中的外键,您只需创建QuoteProduction记录:
var qp = new QuoteProduction
{
QuoteGeneral = quoteGeneral
};
The quoteGeneral
object doesn't have to have been saved first.
quoteGeneral对象不必先保存。
Then when you save the quoteGeneral
record the associated QuoteProduction
record will get saved at the same time.
然后,当您保存quoteGeneral记录时,相关的QuoteProduction记录将同时保存。
#2
1
I would like a record to be automatically created in the QuoteProduction table whenever one is entered into the QuoteGeneral table.
我希望只要在QuoteGeneral表中输入一个记录就会在QuoteProduction表中自动创建。
Create trigger trg_test
on dbo.QuoteGeneral
after insert
as
begin
Set nocount on
insert into dbo.QuoteProduction
select requiredcolumns
from
inserted i
End
#3
0
After you do .SaveChanges() you should be able to get the id of that object and then use that to add the child table.
执行.SaveChanges()之后,您应该能够获取该对象的id,然后使用它来添加子表。
#1
3
As the QuoteGeneralID is a foreign key into the QuoteGeneral table you can simply create the QuoteProduction record:
由于QuoteGeneralID是QuoteGeneral表中的外键,您只需创建QuoteProduction记录:
var qp = new QuoteProduction
{
QuoteGeneral = quoteGeneral
};
The quoteGeneral
object doesn't have to have been saved first.
quoteGeneral对象不必先保存。
Then when you save the quoteGeneral
record the associated QuoteProduction
record will get saved at the same time.
然后,当您保存quoteGeneral记录时,相关的QuoteProduction记录将同时保存。
#2
1
I would like a record to be automatically created in the QuoteProduction table whenever one is entered into the QuoteGeneral table.
我希望只要在QuoteGeneral表中输入一个记录就会在QuoteProduction表中自动创建。
Create trigger trg_test
on dbo.QuoteGeneral
after insert
as
begin
Set nocount on
insert into dbo.QuoteProduction
select requiredcolumns
from
inserted i
End
#3
0
After you do .SaveChanges() you should be able to get the id of that object and then use that to add the child table.
执行.SaveChanges()之后,您应该能够获取该对象的id,然后使用它来添加子表。