我有一些表来指定标准电子邮件消息以及发送它们的地址:
Email.Message
-------------
MessageId
Name
FromAddressId
Email.Address
-------------
AddressId
这些表使用以下类在代码中建模:
public class EmailAddress
{
public int AddressId { get; set; }
}
public class EmailMessage
{
public int MessageId { get; set; }
public string Name { get; set; }
public int FromAddressId { get; set; }
public virtual EmailAddress FromAddress { get; set; }
}
var message = Db.EmailMessages
.Where(m => m.Name == name)
.Include(m => m.FromAddress)
.SingleOrDefault();
我正在使用Fluent API来指定关系:
internal class EmailMessageMap : EntityTypeConfiguration<EmailMessage>
{
public EmailMessageMap()
{
ToTable("Message", "Email");
HasKey(p => p.MessageId);
// What do I put here?
}
}
如何设置联接以使用FromAddressId
类的EmailMessage
?我尝试过这些选项:
HasRequired(p => p.FromAddress).WithRequiredPrincipal(); // uses LEFT OUTER JOIN
HasRequired(p => p.FromAddress).WithRequiredDependent(); // uses INNER JOIN
这些导致创建的SQL使用LEFT OUTER JOIN
或INNER JOIN
,但他们总是尝试将Message.MessageId
加入Address.AddressId
,这是不正确的。如何使用正确的列(FromAddressId
)?我需要替换// What do I put here?
,否则我会得到以下异常:
无效的列名'FromAddress_AddressId'
编辑:我可以在[ForeignKey("FromAddressId")]
属性的顶部放置一个FromAddress
,但它有效:
[Extent1].[FromAddressId] = [Extent2].[AddressId]
进展!现在,我如何在Fluent API中执行此操作?
答案 0 :(得分:1)
现在,我如何在Fluent API中执行此操作?
HasRequired(p => p.FromAddress)
.WithMany()
.HasForeignKey(p => p.FromAddressId);