I am not 100% sure what kind of db schema you are trying to achieve here. My assumption is that you want to have foreign key to the same table. In your comment, you said you want a one to one relation ship. Technically this is not possible. Because when you try to insert the first record, You need to provide a value for your foreign key column. The value should be an Id of an existing record. Wait... We don't have any records yet !
Since 1-1 is not really practical here, You should be doing a one to zero/one relationship. That means your foreign key column should be nullable so that you can insert your first record with NULL
value in your foreign key column.
I am having a little hard time understanding your model & property names. So i am going to use a general class/table which everyone can understand, but with your specific requirement ( Self reference foreign key)
I am not using data annotations, I am using Fluent API
The silly business requirement/assumption is that :
- One Person may or may not have a Parent
- One Person may or may not have a Kid
So our entity class will look like this
public class Person
{
public int Id { set; get; }
public string Name { set; get; }
public virtual Person Parent { set; get; }
public virtual Person Kid { set; get; }
}
Now to use fluent API to control the relationships, We need to go to our DBContext class and override the OnModelCreating
method
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyConnectionStringName") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().HasKey(d => d.Id)
.HasOptional(m => m.Kid).WithOptionalPrincipal(d=>d.Parent);
base.OnModelCreating(modelBuilder);
}
public DbSet<Person> Persons { set; get; }
}
This will create the table with 3 Columns, ID, Name and Parent_Id (NULLABLE, Foreign Key relationship to ID
of same table)
I can insert data like this
var db = new MyDbContext();
var myMom = new Person {Name = "Indira"};
var me = new Person {Name = "Shyju", Parent = myMom};
var myDaughter = new Person { Name = "Gauri", Parent = me};
db.Persons.Add(myMom);
db.Persons.Add(me);
db.Persons.Add(myDaughter);
db.SaveChanges();
And you will have your data with ParentId column having a foreign key to the ID column of the same table.
