阻止 flutter 生成双外键
当我从我的一个表中生成数据库作为每个字段的 2 个时,我不明白为什么
我有 fk 键,我希望它们看起来如何(StudentId),但它也生成了他们想要的键看起来(student_id)
public class PermissionLevel
{
public virtual int PermissionLevelId { get; private set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
public virtual Permission Permission { get; set; }
}
public class PermissionMap : ClassMap<Permission>
{
public PermissionMap()
{
Table("Permissions");
Id(x => x.PermissionId).Column("PermissionId");
Map(x => x.Name).NvarcharWithMaxSize().Not.Nullable();
HasMany(x => x.PermissionLevels);
}
}
public class PermissionLevelMap : ClassMap<PermissionLevel>
{
public PermissionLevelMap()
{
Table("PermissionLevels");
Id(x => x.PermissionLevelId).Column("PermissionLevelId");
References(x => x.Permission).Not.Nullable().Column("PermissionId");
References(x => x.Student).Not.Nullable().Column("StudentId");
References(x => x.Course).Not.Nullable().Column("CourseId");
}
}
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Table("Students");
Id(x => x.StudentId).Column("StudentId");
HasMany(x => x.PermissionLevels);
}
}
我的所有看起来都是这样,我得到
(来源:gyazo.com)
ISessionFactory fluentConfiguration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("Connection")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Framework.Data.Mapping.StudentMap>())
.ExposeConfiguration(BuidSchema)
.BuildSessionFactory();
When I generate my database from fluent one of my tables as 2 of every field and I can't figure out why
like I have the fk keys how I want them too look(StudentId) but it also generates the keys how they want it to look(student_id)
public class PermissionLevel
{
public virtual int PermissionLevelId { get; private set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
public virtual Permission Permission { get; set; }
}
public class PermissionMap : ClassMap<Permission>
{
public PermissionMap()
{
Table("Permissions");
Id(x => x.PermissionId).Column("PermissionId");
Map(x => x.Name).NvarcharWithMaxSize().Not.Nullable();
HasMany(x => x.PermissionLevels);
}
}
public class PermissionLevelMap : ClassMap<PermissionLevel>
{
public PermissionLevelMap()
{
Table("PermissionLevels");
Id(x => x.PermissionLevelId).Column("PermissionLevelId");
References(x => x.Permission).Not.Nullable().Column("PermissionId");
References(x => x.Student).Not.Nullable().Column("StudentId");
References(x => x.Course).Not.Nullable().Column("CourseId");
}
}
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Table("Students");
Id(x => x.StudentId).Column("StudentId");
HasMany(x => x.PermissionLevels);
}
}
all mine look like that and I get
(source: gyazo.com)
ISessionFactory fluentConfiguration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("Connection")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Framework.Data.Mapping.StudentMap>())
.ExposeConfiguration(BuidSchema)
.BuildSessionFactory();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法重复该问题,但请尝试以下操作:在您的配置中,更改映射以包含外键名称的约定,例如:
有了该约定,您可以从各个映射文件中删除 Column() 调用并达到相同的结果。由于我无法重现您的确切问题,我希望这也能解决问题(我有一种预感,Fluent 的 AutoMap 功能正在以某种方式混合在一起,但看起来您的代码不允许这样做,所以这确实只是一种预感)。
I wasn't able to duplicate the issue, but try this: in your configuration, change the mappings to include a convention for the foreign key names like:
With that in place, you can remove the Column() call from the individual mapping files and achieve the same result. Since I can't reproduce your exact issue, I'm hoping this will also clear up the problem (I have a hunch that Fluent's AutoMap feature is getting in the mix somehow but it doesn't look like your code is allowing for this, so it really is just a hunch).