1

我在两个不同的文件上有我的模型和映射,在我的映射类上我这样做:

public class UserEmailMap : EntityTypeConfiguration<UserEmail> {
    HasKey(t => new { t.UserId, t.EmailId });

    Property(t => t.UserId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    Property(t => t.EmailId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    ToTable("UsersEmails");

    Property(t => t.UserId).HasColumnName("UserId");
    Property(t => t.EmailId).HasColumnName("EmailId");
    Property(t => t.IsPrimary).HasColumnName("IsPrimary");

    HasRequired(t => t.Email)
        .WithMany(t => t.UserEmails)
        .HasForeignKey(d => d.EmailId);
    HasRequired(t => t.User)
        .WithMany(t => t.UserEmails)
        .HasForeignKey(d => d.UserId);
}

现在,视觉工作室抱怨这些行:

Property(t => t.UserId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

Property(t => t.EmailId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

它强调了两者DatabaseGeneratedOption.None

有了这个消息:Argument type 'System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption' is not assignable to parameter type 'System.Nullable<System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption>'

我试过这个:

Nullable<DatabaseGeneratedOption> x1 = DatabaseGeneratedOption.None;
DatabaseGeneratedOption? x2 = DatabaseGeneratedOption.None;

Property(t => t.UserId)
    .HasDatabaseGeneratedOption(x1);

Property(t => t.EmailId)
    .HasDatabaseGeneratedOption(x2);

这是意想不到的部分:

Argument type 'System.Nullable<System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption> [mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]' is not assignable to parameter type 'System.Nullable<System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption> [mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]'

x1和上的相同消息x2

不会导致编译器错误,但我仍然想解决这个问题。有什么想法吗?

4

1 回答 1

2

通过添加演员修复了这个问题

Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None
              as System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption?);

不漂亮,但 Visual Studio 不再抱怨了。

于 2015-11-10T17:09:45.060 回答