Nullable DatabaseGeneratedOption不能分配给参数类型Nullable DatabaseGeneratedOption

时间:2013-09-28 02:08:51

标签: c# .net c#-4.0 data-annotations

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

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);
}

现在,visual studio正在抱怨这些内容:

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]'

x1x2上的相同消息。

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

1 个答案:

答案 0 :(得分:2)

通过添加演员

解决了这个问题
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None
              as System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption?);

不漂亮,但Visual Studio停止了抱怨。