简体   繁体   English

EF Table-Per-Type:将新的子代添加到现有父代

[英]EF Table-Per-Type: Add new child to an existing parent

I'm new with ASP.NET MVC Code First, I have implemented a table-per-type hierarchy that goes something like this: 我是ASP.NET MVC代码的新手,首先,我实现了每个类型的表层次结构,如下所示:

public abstract class Request
{
    public int RequestId {gets;set;}
    public DateTime CreationDate {get;set;}
    public string CreatedBy {gets;set;}
    public bool Active {Get;set;}
}

public class RequestChildA : Request
{
    public string RequestChildAProp1 {get;set;}
    public string RequestChildAProp2 {get;set;}
    ...
}

public class RequestChildB : Request
{
    public string RequestChildBProp1 {get;set;}
    public string RequestChildBProp1 {get;set;}
    ...
}


Request table
+---------------+-------------------------+------------+--------+
|    RequestId  |       CreationDate      |  CreatedBy | Active | 
+---------------+-------------------------+------------+--------+
| 1             | 2019-08-19 09:40:39.817 | HydroFlask |    1   |
+---------------+-------------------------+------------+--------+

RequestChildA table
+---------------+-------------------------+---------------------+
|    RequestId  |   RequestChildAProp1    |  RequestChildAProp1 | 
+---------------+-------------------------+---------------------+
| 1             |         ValueA          |   ValueB            |
+---------------+-------------------------+---------------------+

ChildA and ChildB can share the same request. ChildA和ChildB可以共享同一请求。 My question is how can I add a NEW ChildB to an existing Request that also has an existing ChildA. 我的问题是如何将NEW ChildB添加到也具有现有ChildA的现有请求中。

I have tried the usual: 我尝试了通常的方法:

var childB = new childB { RequestChildBProp1 = "Value1", RequestChildBPro2 = "Value2" };
childB.RequestId = 1;
db.RequestChildB.Add(childB);
db.SaveChanges();

It is completed with no errors, but it creates a new Request. 它已完成,没有错误,但是创建了一个新的请求。 I need a new ChildB that is the same RequestId as the existing. 我需要一个与现有ID相同的新ChildB。 Please help! 请帮忙!

My design might be wrong, please bear with me. 我的设计可能有误,请耐心等待。 Thank you! 谢谢!

Try the following, not tested: 请尝试以下未经测试的方法:


public class Request
{
    public int RequestId {gets;set;}
    public DateTime CreationDate {get;set;}
    public string CreatedBy {gets;set;}
    public bool Active {Get;set;}
}

public class RequestChildA
{
    public string RequestChildAProp1 {get;set;}
    public string RequestChildAProp2 {get;set;}
    ...
    public virtual Request Request {get; set;}

}

public class RequestChildB
{
    public string RequestChildBProp1 {get;set;}
    public string RequestChildBProp1 {get;set;}
    ...
    public virtual Request Request {get; set;}
}

I think what you need is creating your RequestChildA and RequestChildB like this: 我认为您需要像这样创建RequestChildARequestChildB

public class RequestChildB
{
    public virtual Request Request {get;set;}

    public string RequestChildBProp1 {get;set;}
    public string RequestChildBProp1 {get;set;}
}

and insert data like this: 并像这样插入数据:

db.RequestChildB.Add(new RequestChildB
{
    Request = db.Request.Find(1),
    //...other properties
});

db.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM