Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不是bug,关于CodeFirst的疑问 #1887

Open
BI7PRK opened this issue Sep 19, 2024 · 3 comments
Open

不是bug,关于CodeFirst的疑问 #1887

BI7PRK opened this issue Sep 19, 2024 · 3 comments

Comments

@BI7PRK
Copy link

BI7PRK commented Sep 19, 2024

问题描述及重现代码:

目的自定义外键字段 Pid 而非默认的 SchedulesId

 public class ScheduleTasks
 {
     [Column(IsIdentity = true, IsPrimary = true)]
     public int Id { get; set; }
     public int Pid { get; set; }
    ....
 }

 freeSql.CodeFirst.Entity<Schedules>(p =>
  {
      p.HasKey(a => a.Id);
      p.Property(a => a.Name);
      p.Property(a => a.Description);
      p.Property(a => a.IsActive);
      p.Property(a => a.CreateTime);
      p.HasMany(a => a.ScheduleTasks).HasForeignKey(a => a.Pid); //自定义外键
      p.ToTable(nameof(Schedules));
  });

 freeSql.CodeFirst.SyncStructure<ScheduleTasks>();

执行后数据库只生成 ScheduleTasks 这个表,Schedules却没有生成。怎么回事呢?

数据库版本

MySql 8.0

安装的Nuget包

FreeSql v3.2.83
FreeSql.DbContext v3.2.83

.net framework/. net core? 及具体版本

net8

@2881099
Copy link
Collaborator

2881099 commented Sep 19, 2024

对于code first来讲,他们都是独立的单体,需要分别同步结构

@BI7PRK
Copy link
Author

BI7PRK commented Sep 19, 2024

对于code first来讲,他们都是独立的单体,需要分别同步结构

应该如何操作呢?

尝试一(无效):

  freeSql.CodeFirst.Entity<Schedules>(p =>
   {
       p.HasKey(a => a.Id);
       p.Property(a => a.Name);
       p.Property(a => a.Description);
       p.Property(a => a.IsActive);
       p.Property(a => a.CreateTime);
       p.HasMany(a => a.ScheduleTasks).HasForeignKey(a => a.Pid);
       p.ToTable(nameof(Schedules));
   }).SyncStructure();

尝试二(无效):

public class SchedulesConfiguration : IEntityTypeConfiguration<Schedules>
{
    public void Configure(EfCoreTableFluent<Schedules> p)
    {
         p.HasKey(a => a.Id);
         p.Property(a => a.Name);
         p.Property(a => a.Description);
         p.Property(a => a.IsActive);
         p.Property(a => a.CreateTime);
         p.HasMany(a => a.ScheduleTasks).HasForeignKey(a => a.Pid);
         p.ToTable(nameof(Schedules));
    }
}

fsql.CodeFirst.ApplyConfiguration(new SchedulesConfiguration());

@2881099
Copy link
Collaborator

2881099 commented Sep 19, 2024

freeSql.CodeFirst.SyncStructure<ScheduleTasks>();
freeSql.CodeFirst.SyncStructure<Schedules>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants