EF Code First One to Many and One-to-One Feedback

I am trying to create one-to-many relationships and undo one-to-one relationships using code in the first place. This is what i am trying to do

1) One-to-many between the two classes and works as expected.

    public class X
    {
        [Key]
        public int XId { get; set; }
        public ICollection<Y> Y { get; set; }

    }

    public class Y
    {
        [Key]
        public int YId { get; set; }
        public int XId { get; set; }
        public X X { get; set; }
    }

    public class DataContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Y>()
                .HasRequired(y => y.X)
                .WithMany(x => x.Y)
                .HasForeignKey(y => y.XId);
        }
    }

Now what I want to do is create a reciprocal relationship between Y and X, so that X will contain the foreign key Y ... How is this possible? This is what I am trying to do, and it causes a plurality error.

       public class X
        {
            [Key]
            public int XId { get; set; }
            public ICollection<Y> Y { get; set; }
            public int YId {get; set; }
            [ForiegnKey("YId")]
            public Y YOptional { get; set; }
        }

        public class Y
        {
            [Key]
            public int YId { get; set; }
            public int XId { get; set; }
            public X X { get; set; }
            public X XOptional {get; set; }
        }

        public class DataContext : DbContext
        {
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Y>()
                    .HasRequired(y => y.X)
                    .WithMany(x => x.Y)
                    .HasForeignKey(y => y.XId);

                modelBuilder.Entity<X>()
                    .HasOptional(x => x.YOptional)
                    .WithOptionalDependent(y=> y.XOptional);
            }
        }
+4
source share
2 answers

You cannot have a relationship between two objects that are different from either end. Thus, you cannot do 1: * from one direction and 1: 1 from another.

, , 1:1 . .

, , , .

, 0..1: * ( ). (, "" ), , (, " " ).

[zero one] to many. , Y nullable int. EF , .

public class X
  {
    [Key]
    public int XId { get; set; }
    public ICollection<Y> Y { get; set; }

  }

  public class Y
  {
    [Key]
    public int YId { get; set; }
    public int? XId { get; set; }
    public X X { get; set; }
  }

  public class DataContext : DbContext
  {
    public DbSet<X> XSet { get; set; }
    public DbSet<Y> YSet { get; set; }
   }

, . , , , , - .

enter image description here

+5

, :

a User, Single, . , 1:1 User a Single, , "". , . "" .

, User , :

public class User
{
    public User()
    {
        this.Singles = new HashSet<Single>();
    }

    public int UserId { get; set; }
    public string Name { get; set; }

    public Single Single { get; set; }

    public virtual ICollection<Single> Singles { get; set; }
}

public class Single
{
    public int SingleId { get; set; }
    public string Name { get; set; }
    public virtual User User { get; set; }
    public int SuperUserId { get; set; }
    public User SuperUser { get; set; }
}

:

1: User

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>().HasMany(u => u.Singles)
        .WithRequired(s => s.SuperUser).HasForeignKey(s => s.SuperUserId);
    modelBuilder.Entity<User>().HasOptional(s => s.Single)
        .WithOptionalPrincipal(s => s.User).Map(m => m.MapKey("UserId"));
}

Single , UserId SuperUserId. User Single User.Single User.Singles:

var superUser = new User { Name = "superUser1" };
var single = new Single { Name = "single" };
superUser.Singles.Add(single);
db.Users.Add(superUser);
superUser.Single = single;
db.SaveChanges();

EF User, Single , User.

2: Single

Single 1:1:

modelBuilder.Entity<User>().HasOptional(s => s.Single)
    .WithOptionalDependent(s => s.User).Map(m => m.MapKey("SingleId"));

Single (SuperUserId) User (SingleId). , EF

.

, : Single , User, User , Single Singles. , Single :

var superUser = new User { Name = "superUser1" };
var single = new Single { Name = "single" };
superUser.Singles.Add(single);
db.Users.Add(superUser);
db.SaveChanges();

superUser.Single = single;
db.SaveChanges();

TransactionScope, , .


, 1:1 . API WithOptionalDependent WithOptionalPrincipal HasForeignKey. , API. .

+2

Source: https://habr.com/ru/post/1568121/


All Articles