3

All possibilities to change the database model with Fluent API i found so far assume that the property which i want to edit exist also as a property in the entity. But i don't know how to change attributes of a property, which is not a property in the entity class.

Example:

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<Address> Addresses { get; set; }

    public Person()
    {
        this.Addresses = new List<Address>();
    }
}
public class Address
{
    public int AddressId { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

Now Code First creates a database where the Addresses table has PersonId as a FK, so having a 1-to-* relationship (what is what i want). But the FK PersonId in Addresses is nullable, and i can't access this FK in Fluent API since there is no property. How can i change this FK to be not-nullable?

Also, at the moment when i want to delete a Person, EF don't let me do this since there is an associated address. I thought the behaviour is:

  • nullable FK: delete the record in question, set the child record FK to null
  • not-nullable: error, or delete record together with all associated records (depending on cascade rules)

Is this not correct?

user3343508
  • 79
  • 1
  • 7

2 Answers2

4

The following Fluent API directive did it for me, so to achieve

  • 1-to-* relationship
  • FK not nullable
  • FK not defined as a property in the entity class
  • cascade delete (deleting the parent deletes all associated records, here Addresses)

the following statement has to be included in OnModelCreating

modelBuilder.Entity<Person>().HasMany(a => a.Addresses).WithRequired().WillCascadeOnDelete(true);
user3343508
  • 79
  • 1
  • 7
0

For first you must make foreign key nullable.

Here is how

For second you will have to use the fluent API to do this.

Try adding the below to your DB Context

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

    modelBuilder.Entity<Person>()
        .HasOptional(a => a.Address)
        .WithOptionalDependent()
        .WillCascadeOnDelete(true);
    }

For further detail see this link

Community
  • 1
  • 1
rollo
  • 305
  • 3
  • 14
  • I'm sorry, i messed up the topic. See the detailed opening post: actually i want a non-nullable FK. For the second: When i use this, Code First wants to drop the FK column on Addresses, and add a FK in the Person table, so i don't have an 1-to-* relationship anymore – user3343508 Aug 31 '14 at 14:39
  • i got it! i'll create an answer and mark it as accepted, although your post gave the inspiration, so thank you! – user3343508 Aug 31 '14 at 15:10
  • Can you kindly tell me if this is expected behaviour. https://stackoverflow.com/questions/45571025/how-to-correctly-set-foreign-key-constraint-in-one-to-many-relationship-in-code/45572561#45572561 – Unbreakable Aug 08 '17 at 19:50