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

时间:2011-01-26 08:56:46

标签: 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; }
}

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

在我的MVC Web App中,我显示了应用程序列表,然后单击一个名为“Create Instance”的链接。

我选择此实例所针对的客户,然后点击保存。

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

由于我使用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();

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

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

我有正确的外键关系和设置。

1 个答案:

答案 0 :(得分: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; }
}

然后你可以像这样保存:

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