实体框架代码首先,外键/对象不应该保存

Mar*_*nHN 2 c# entity-framework code-first

我有三个课程,如下:

[DataContract]
public class ApplicationDto : BusinessBase<int>
{
    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    [DataMember]
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the description.
    /// </summary>
    /// <value>The description.</value>
    [DataMember]
    public string Description { get; set; }

    /// <summary>
    /// Gets or sets the development startdate.
    /// </summary>
    /// <value>The development startdate.</value>
    [DataMember]
    public DateTime DevelopmentStartdate { get; set; }

    /// <summary>
    /// Gets or sets the launch date.
    /// </summary>
    /// <value>The launch date.</value>
    [DataMember]
    public DateTime LaunchDate { get; set; }
}

[DataContract]
public class CustomerDto : BusinessBase<int>
{
    /// <summary>
    /// Gets or sets the name of the user.
    /// </summary>
    /// <value>The name of the user.</value>
    [DataMember()]
    public string UserName { get; set; }

    /// <summary>
    /// Gets or sets the first name.
    /// </summary>
    /// <value>The first name.</value>
    [DataMember()]
    public string FirstName { get; set; }

    /// <summary>
    /// Gets or sets the last name.
    /// </summary>
    /// <value>The last name.</value>
    [DataMember()]
    public string LastName { get; set; }

    /// <summary>
    /// Gets or sets the name of the company.
    /// </summary>
    /// <value>The name of the company.</value>
    [DataMember()]
    public string CompanyName { get; set; }

    /// <summary>
    /// Gets or sets the phone.
    /// </summary>
    /// <value>The phone.</value>
    [DataMember()]
    public string Phone { get; set; }

    /// <summary>
    /// Gets or sets the email.
    /// </summary>
    /// <value>The email.</value>
    [DataMember()]
    public string Email { get; set; }

    /// <summary>
    /// Gets or sets the address1.
    /// </summary>
    /// <value>The address1.</value>
    [DataMember()]
    public string Address1 { get; set; }

    /// <summary>
    /// Gets or sets the address2.
    /// </summary>
    /// <value>The address2.</value>
    [DataMember()]
    public string Address2 { get; set; }

    /// <summary>
    /// Gets or sets the city.
    /// </summary>
    /// <value>The city name.</value>
    [DataMember()]
    public string City { get; set; }

    /// <summary>
    /// Gets or sets the state region.
    /// </summary>
    /// <value>The state region.</value>
    [DataMember()]
    public string StateRegion { get; set; }

    /// <summary>
    /// Gets or sets the zip code.
    /// </summary>
    /// <value>The zip code.</value>
    [DataMember()]
    public string ZipCode { get; set; }

    /// <summary>
    /// Gets or sets the country id.
    /// </summary>
    /// <value>The country id.</value>
    [DataMember()]
    public int CountryId { get; set; }

    /// <summary>
    /// Gets or sets the ean number.
    /// </summary>
    /// <value>The ean number.</value>
    [DataMember()]
    public string EanNumber { get; set; }

    /// <summary>
    /// Gets or sets the vat number.
    /// </summary>
    /// <value>The vat number.</value>
    [DataMember()]
    public string VatNumber { get; set; }

    /// <summary>
    /// Gets or sets the time zone id.
    /// </summary>
    /// <value>The time zone id.</value>
    [DataMember()]
    public string TimeZoneId { get; set; }
}

[DataContract]
public class ApplicationInstanceDto : BusinessBase<int>
{
    /// <summary>
    /// Gets or sets the customer id.
    /// </summary>
    /// <value>The customer id.</value>
    [DataMember]
    public int CustomerId { get; set; }

    /// <summary>
    /// Gets or sets the application id.
    /// </summary>
    /// <value>The application id.</value>
    [DataMember]
    public int ApplicationId { get; set; }

    /// <summary>
    /// Gets or sets the application.
    /// </summary>
    /// <value>The application.</value>
    [DataMember]
    public ApplicationDto Application { get; set; }

    /// <summary>
    /// Gets or sets the customer.
    /// </summary>
    /// <value>The customer.</value>
    [DataMember]
    public CustomerDto Customer { get; set; }

    /// <summary>
    /// Gets or sets the initial version id.
    /// </summary>
    /// <value>The initial version id.</value>
    [DataMember]
    public int InitialVersionId { get; set; }

    /// <summary>
    /// Gets or sets the current version id.
    /// </summary>
    /// <value>The current version id.</value>
    [DataMember]
    public int CurrentVersionId { get; set; }

    /// <summary>
    /// Gets or sets the name of the unique instance.
    /// </summary>
    /// <value>The name of the unique instance.</value>
    [DataMember]
    public string UniqueInstanceName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

假设我的数据库中有2个应用程序,还有几个客户.

在我的MVC Web App中,我显示了应用程序列表,然后单击名为"Create Instance"的链接.

我选择此实例所针对的客户,然后单击"保存".

在EF Code First中,默认情况下它始终保存相关对象 - 在本例中为Application和Customer.

因为我使用AJAX调用这样做,所以我只将应用程序和客户的Id发送回我的Controller.查看数据库,这就是创建ApplicationInstance所需的全部内容.

我做这个'欺骗':

var appInstance = new ApplicationInstanceDto();
            appInstance.InitialVersionId = 1;
            appInstance.CurrentVersionId = 2;
            appInstance.ApplicationId = 1;
            appInstance.CustomerId = 1;
            appInstance.UniqueInstanceName = "test";

db.ApplicationInstances.Add(appInstance);
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

但是,当然,我从数据库中得到一个异常,告诉我Applications表中的name列不允许null.

有什么方法可以欺骗更多,并避免相关对象的整个插入?

我有外键关系,这样设置正确.

Mor*_*avi 5

我建议您将关联更改为外键关联而不是独立(现在是):

public class Application
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ApplicationInstance
{
    public int Id { get; set; }
    public int ApplicationId { get; set; }
    public int CustomerId { get; set; }

    public Application Application { get; set; }
    public Customer Customer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样保存:

var appInstance = new ApplicationInstance();
appInstance.CustomerId = customerId;
appInstance.ApplicationId = applicationId;

db.ApplicationInstances.Add(appInstance);
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

更新:

您可以使用ForeignKeyAttribute数据注释将FK列与其导航属性相关联:

public class ApplicationDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class CustomerDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ApplicationInstanceDto
{
    public int Id { get; set; }
    public int ApplicationId { get; set; }
    public int CustomerId { get; set; }

    [ForeignKey("ApplicationId")]
    public ApplicationDto Application { get; set; }

    [ForeignKey("CustomerId")]
    public CustomerDto Customer { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)