1

There are two entities:

  1. Group
  2. Yuvak - Person

"Other" person is designed in back-end who has no group. (null)

A Yuvak - Person will always have one HomeGroup. (1=>1) And will have no groups to control.

A Nirikshak(Head) - Person will always have one HomeGroup. (1=>1) But he will also have multiple groups to control - GroupsOfNirikshak. (1=>Many)

A Group will have multiple Yuvaks (1=>Many)

and all groups mostly will have only one Head. (Initially a new group might not have any head but zero or more yuvaks-persons.)

[Table("Group")]
public class Group
{
    [Key]
    public int Id { get; set; }
    .....       
    public virtual List<Yuvak> Yuvaks { get; set; }
    [ForeignKey("Nirikshak")]
    public int? NirikshakId { get; set; }
    public virtual Yuvak Nirikshak { get; set; }
}

[Table("Yuvak")]
public class Yuvak
{
    [Key]
    public int Id { get; set; }
    .....
    [ForeignKey("HomeGroup")]
    public int? HomeGroupId { get; set; }
    public virtual Group HomeGroup { get; set; }
    public virtual List<Group> GroupsOfNirikshak { get; set; }
}

I already has provided two foreign keys for 1=>1 relationships (nullable) in both entities. And now to manage many to many relationship it should automatically create a third table with "Yuvak_Id" and "Group_Id" columns if they are not null. But as here the FKs are null; instead of creating a third table it adds a foreign key column in both the tables.(Group:"Yuvak_Id" and Yuvak:"Group_Id")

What should I do so that to maintain Yuvak-HomeGroup it should use above provided foreign keys only and for many to many relationship (Head-GroupsOfNirikshak & Group-Yuvaks )it should create a seperate table.

Can I create a separate table for many to many relationship like : (ID,YuvakID,GroupID) How can I do that?

I tried to do that but got different errors like below :

  1. The navigation property 'HomeGroup' declared on type 'YTKGoshthiManagment.Models.Yuvak' has been configured with conflicting foreign keys.
  2. Multiplicity is not valid in Role 'Yuvak_HomeGroup_Target' in relationship 'Yuvak_HomeGroup'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

.....

and so on.

2 Answers 2

1

Use the "Fluent Api" !

In your context class write (for example) :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Group>()
                 .HasOptional(t => t.Nirikshak)
                 .WithMany(t => t.GroupsOfNirikshak)
                 .HasForeignKey(t => t.NirikshakId);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can remove the annotations on the classes and properties. Once you have a Model-based Class on another, Entity Framework will automatically create a foreign key relationship on it. It will process Yuvak as a node on the Group Graph object. You need not declare the annotations since EF will do that for you automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.