繁体   English   中英

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

[英]Entity Framework Code First, foreign key/object should not save

我有三节课,像这样:

[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; }
}

假设我的数据库中存在2个应用程序,并且有几个客户。

在我的MVC Web应用程序中,我显示应用程序列表,然后单击一个名为“创建实例”的链接。

我选择该实例用于的客户,然后单击“保存”。

默认情况下,在EF Code First中,它始终保存相关对象-在这种情况下为应用程序和客户。

因为我使用AJAX调用来执行此操作,所以我只将应用程序和客户的ID发送回我的控制器。 查看数据库,这就是创建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();

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

有什么办法可以使我作弊更多,并避免整个插入相关对象?

我有外键关系,并且这种关系设置正确。

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

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; }
}

然后您可以像这样保存:

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

db.ApplicationInstances.Add(appInstance);
db.SaveChanges();

更新:

您可以使用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; }
} 

暂无
暂无

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

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