我的问题与Is it possible to have a relation where the foreign key is also the primary key?类似,但我必须使用Fluent API执行此操作。
我基本上和问题中描述的情况相同,但由于我们的编码标准,我无法在我的域模型上使用注释。这是一些代码(澄清):
域类:
public class Table1
{
public long ID { get; set; }
public int SubTableType { get; set; }
...
public Table2 Table2 { get; set; }
public Table3 Table3 { get; set; }
public List<Table4> Table4s { get; set; }
public List<Table5> Table5s { get; set; }
}
public class Table2
{
public long ID { get; set; }
public string Location { get; set; }
public string Task { get; set; }
...
public Table1 Table1 { get; set; }
public Table6 Table6 { get; set; }
public List<Table7> Table7s { get; set; }
}
public class Table3
{
public long ID { get; set; }
public string DescriptionAndLocation { get; set; }
...
public Table1 Table1 { get; set; }
}
配置类:
internal class Table1Configuration : EntityTypeConfiguration<Table1>
{
public Table1Configuration()
{
ToTable("Table1");
HasKey(so => so.ID);
Property(so => so.SubTableType)
.IsRequired();
Property(so => so.ID)
.IsRequired()
.HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
...
}
}
internal class Table2Configuration : EntityTypeConfiguration<Table2>
{
public Table2Configuration()
{
ToTable("Table2");
HasKey(bc => bc.ID);
Property(bc => bc.ID)
.IsRequired();
Property(bc => bc.Location)
.IsOptional()
.HasColumnType("nvarchar")
.HasMaxLength(50);
Property(bc => bc.Task)
.IsOptional()
.HasColumnType("nvarchar")
.HasMaxLength(4000);
...
HasRequired(bc => bc.Table1)
.WithOptional(so => so.Table2);
HasRequired(bc => bc.Table8)
.WithMany(bot => bot.Table2s)
.HasForeignKey(bc => bc.Tabe8ID);
}
}
internal class Table3Configuration : EntityTypeConfiguration<Table3>
{
public Table3Configuration()
{
ToTable("Table3");
HasKey(hic => hic.ID);
Property(hic => hic.DescriptionAndLocation)
.IsOptional()
.HasColumnType("nvarchar")
.HasMaxLength(4000);
Property(hic => hic.ID)
.IsRequired()
.HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
HasRequired(hic => hic.Table1)
.WithOptional(so => so.Table3);
}
}
当我运行此代码时,我收到错误:
Invalid column name 'Table2_ID'.
答案 0 :(得分:1)
我会尝试这样的事情:
$(function() {
var $sidebar = $("#sidebar"),
$window = $(window),
offset = $sidebar.offset(),
topPadding = 75;
var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
var footer_top = $("#footer").offset().top;
var div_top = $('#sticky-anchor').offset().top;
var div_height = $("#sidebar").height();
var desiredHeight = _docHeight - footer_top;
$window.scroll(function()
{
if ($window.scrollTop() > offset.top)
{
$sidebar.stop().animate({
marginTop: $window.scrollTop() - offset.top + topPadding
});
}
else if($window.scrollTop > desiredHeight)
{
$sidebar.stop().animate({
marginTop: $window.scrollTop()-100
});
}
else {
$sidebar.stop().animate({
marginTop: 0
});
}
});
});
答案 1 :(得分:1)
你所问的是所谓的Shared Primary Key Associations,它是一对一关系的标准(并且得到更好支持的)EF6模型。
您应该删除用于定义影子FK属性(您不需要)的ID
调用,而不是删除MapKey
属性。
由于按惯例名为ID
的属性是PK并且是必需的,基本上所有你需要的是:
HasRequired(hic => hic.Table1)
.WithOptional(so => so.Table2); // or Table3
或明确相当于[Key]
/ [ForeignKey]
组合:
HasKey(hic => hic.ID);
HasRequired(hic => hic.Table1)
.WithOptional(so => so.Table2); // or Table3
正好是文档中Configuring a Required-to-Optional Relationship (One-to–Zero-or-One)的示例。