First, foreign keys/objects should not be saved in Entity Framework code.

c# code-first entity-framework

Question

I'm taking three courses, as follows:

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

Let's imagine my database has 2 apps, along with a number of clients.

I display a list of apps in my MVC Web app and choose the "Create Instance" link.

I click save after selecting the customer this instance is for.

In EF Code First, saving associated objects—in this example, the Application and the Customer—is always done by default.

Since I'm utilizing AJAX calls, I'm simply returning to my Controller the Ids for the application and the customer. That is all I need to build an ApplicationInstance, according to the database.

By doing this, I "cheat"

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();

Naturally, the database throws me an exception informing me that the name field in the Applications table cannot be null.

Can I cheat even more by avoiding the complete insert of related objects?

I have everything set up properly, including foreign key connections.

1
2
1/26/2011 6:31:08 PM

Accepted Answer

I advise switching your associations from being independent (as they are currently) to International Key Associations:

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

You may then save in the following way:

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

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

Update:

You may utilizeForeignKeyAttribute Annotating data to link FK columns to their navigational characteristics:

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; }
} 
5
1/26/2011 6:58:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow