将 API 到 POST 写入多对多关系
Writing an API to POST to a many-to-many relationship
大家晚上好
对于有经验的开发人员来说,这可能是一个简单的问题,但我是 C# 的新手,ASP.Net 和 Entity Framework。我有两个 tables - AppUser 和 CompanySettings。这2个table之间是多对多的关系。
public class AppUser
{
public int Id { get; set; }
public string UserName { get; set; }
public byte[] PasswordHash { get; set; }
public byte[] PasswordSalt { get; set; }
public string KnownAs { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
public DateTime LastActive {get; set;} = DateTime.Now;
public ICollection<Photo> Photos { get; set; }
public virtual ICollection<AppUserCompanySettings> AppUserCompanySettings { get; set; }
}
public class CompanySettings
{
public int Id { get; set; }
public string CompanyName { get; set; }
public string CompanyRegistrationNumber { get; set; }
public bool isActive { get; set; }
public bool isArchived { get; set; }
public virtual ICollection<AppUserCompanySettings> AppUserCompanySettings { get; set; }
}
我还创建了连接 table:
public class AppUserCompanySettings
{
public int Id { get; set; }
public int AppUserId { get; set; }
public virtual AppUser AppUser { get; set; }
public int CompanySettingsId { get; set; }
public virtual CompanySettings CompanySettings { get; set; }
}
我的 API 现在看起来像这样:
public async Task<ActionResult<CompanySettings>> AddNewCompany(CompanySettings companySettings)
{
if (await CompanyExists(companySettings.CompanyRegistrationNumber)) return
BadRequest("Company already exists. Please contact our Support team for assistance");
var user = new AppUser {
Id = 1
};
var company = new CompanySettings
{
CompanyName = companySettings.CompanyName,
CompanyRegistrationNumber = companySettings.CompanyRegistrationNumber,
isActive = companySettings.isActive,
isArchived = companySettings.isArchived,
};
_context.CompanySettings.Add(company);
_context.Update(user);
await _context.SaveChangesAsync();
return company;
}
用户注册 - 这会将用户名和密码写入 AppUser table。他不需要在注册时创建公司。
但是,当他想添加公司(写入公司设置)时,我如何将 API 到 post 写入 CompanySettings table 以及EF 创建的 AppuserCompanySettings table?
目前,如果我调用 API,它会创建公司。但它不会加入 AppUserCompanySettings。
如何更新加入 table?
确保你在你的 DbContext
配置中加入这个:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AppUser>()
.HasMany(x => x.AppUserCompanySettings )
.WithOne(x => x.AppUser)
.HasForeignKey(x => x.AppUserId);
modelBuilder.Entity<CompanySettings>()
.HasMany(x => x.AppUserCompanySettings)
.WithOne(x => x.CompanySettings)
.HasForeignKey(x => x.CompanySettingsId);
modelBuilder.Entity<AppUserCompanySettings>()
.HasKey(x => new {x.AppUserId, x.CompanySettingsId});
}
从您的 AppUserCompanySettings
中替换 public int Id { get; set; }
,因为您的 table 中只有 2 个外键,它们的作用类似于主键
这段代码之后:
var user = new AppUser
{
Id = 1
};
var company = new CompanySettings
{
CompanyName = companySettings.CompanyName,
CompanyRegistrationNumber = companySettings.CompanyRegistrationNumber,
isActive = companySettings.isActive,
isArchived = companySettings.isArchived,
};
你必须这样做:
// adding relationship between entities here
user.AppUserCompanySettings.Add(new AppUserCompanySettings
{
AppUser = user,
CompanySettings = company
});
// if you haven't created a new user but used already existing -
// replace Add with Update
_context.Add(user);
_context.SaveChanges();
也像这样修改 AppUser
和 CompanySettings
中的 Collections
public virtual ICollection<AppUserCompanySettings> AppUserCompanySettings { get; set; } = new List<AppUserCompanySettings>();
大家晚上好
对于有经验的开发人员来说,这可能是一个简单的问题,但我是 C# 的新手,ASP.Net 和 Entity Framework。我有两个 tables - AppUser 和 CompanySettings。这2个table之间是多对多的关系。
public class AppUser
{
public int Id { get; set; }
public string UserName { get; set; }
public byte[] PasswordHash { get; set; }
public byte[] PasswordSalt { get; set; }
public string KnownAs { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
public DateTime LastActive {get; set;} = DateTime.Now;
public ICollection<Photo> Photos { get; set; }
public virtual ICollection<AppUserCompanySettings> AppUserCompanySettings { get; set; }
}
public class CompanySettings
{
public int Id { get; set; }
public string CompanyName { get; set; }
public string CompanyRegistrationNumber { get; set; }
public bool isActive { get; set; }
public bool isArchived { get; set; }
public virtual ICollection<AppUserCompanySettings> AppUserCompanySettings { get; set; }
}
我还创建了连接 table:
public class AppUserCompanySettings
{
public int Id { get; set; }
public int AppUserId { get; set; }
public virtual AppUser AppUser { get; set; }
public int CompanySettingsId { get; set; }
public virtual CompanySettings CompanySettings { get; set; }
}
我的 API 现在看起来像这样:
public async Task<ActionResult<CompanySettings>> AddNewCompany(CompanySettings companySettings)
{
if (await CompanyExists(companySettings.CompanyRegistrationNumber)) return
BadRequest("Company already exists. Please contact our Support team for assistance");
var user = new AppUser {
Id = 1
};
var company = new CompanySettings
{
CompanyName = companySettings.CompanyName,
CompanyRegistrationNumber = companySettings.CompanyRegistrationNumber,
isActive = companySettings.isActive,
isArchived = companySettings.isArchived,
};
_context.CompanySettings.Add(company);
_context.Update(user);
await _context.SaveChangesAsync();
return company;
}
用户注册 - 这会将用户名和密码写入 AppUser table。他不需要在注册时创建公司。
但是,当他想添加公司(写入公司设置)时,我如何将 API 到 post 写入 CompanySettings table 以及EF 创建的 AppuserCompanySettings table?
目前,如果我调用 API,它会创建公司。但它不会加入 AppUserCompanySettings。
如何更新加入 table?
确保你在你的 DbContext
配置中加入这个:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AppUser>()
.HasMany(x => x.AppUserCompanySettings )
.WithOne(x => x.AppUser)
.HasForeignKey(x => x.AppUserId);
modelBuilder.Entity<CompanySettings>()
.HasMany(x => x.AppUserCompanySettings)
.WithOne(x => x.CompanySettings)
.HasForeignKey(x => x.CompanySettingsId);
modelBuilder.Entity<AppUserCompanySettings>()
.HasKey(x => new {x.AppUserId, x.CompanySettingsId});
}
从您的 AppUserCompanySettings
中替换 public int Id { get; set; }
,因为您的 table 中只有 2 个外键,它们的作用类似于主键
这段代码之后:
var user = new AppUser
{
Id = 1
};
var company = new CompanySettings
{
CompanyName = companySettings.CompanyName,
CompanyRegistrationNumber = companySettings.CompanyRegistrationNumber,
isActive = companySettings.isActive,
isArchived = companySettings.isArchived,
};
你必须这样做:
// adding relationship between entities here
user.AppUserCompanySettings.Add(new AppUserCompanySettings
{
AppUser = user,
CompanySettings = company
});
// if you haven't created a new user but used already existing -
// replace Add with Update
_context.Add(user);
_context.SaveChanges();
也像这样修改 AppUser
和 CompanySettings
中的 Collections
public virtual ICollection<AppUserCompanySettings> AppUserCompanySettings { get; set; } = new List<AppUserCompanySettings>();