开发者

EF CTP4: How to tell EF a column is NOT identity

I have a code-first, POCO project in which I am trying to adjust an existing database so that it syncs up with what EF is expecting, given my existing model.

I have these entities:

public class FlaggedDate
{
    [Key]
    public long scheduledDayID { get; set; }
    [Required]
    public DateTime date { get; set; }
    [StringLength(50)]
    [Required]
    public string dateStatus { get; set; }
    [Required]
    public bool isVisit { get; set; }
    [Required]
    public bool hasAvailableSlots { get; set; }
    [Required]
    public bool hasInterviewsScheduled { get; set; }

    // navigation properties
    public ICollection<ScheduledSchool> scheduledSchool { get; set; }
    public ICollection<Interview> interviews { get; set; }
    public ICollection<PartialDayAvailableBlock> partialDayAvailableBlocks { get; set; }
    public Visit visit { get; set; }
    public ICollection<Event> events { get; set; }
}

and

public class Visit
{
    [Key]
    public long flaggedDateScheduledDayID { get; set; }
    [Required]
    public bool isFullDay { get; set; }

    // navigation property
    public FlaggedDate flaggedDate { get; set; }
}

The relationship between these two is 1 : 0|1 -- i.e., FlaggedDate will exist but it may or may not have a corresponding single Visit object.

EF thinks, based on this model, that FlaggedDate should have an extra field, visit_flaggedDateScheduledDayID, which is nullable. I finally realized why: it thinks the Visit field, flaggedDateScheduledDayID, is an identity column. It's not supposed to be an identity column; it's supposed to be a foreign key that connects to FlaggedDate.

I think it does this by convention: I remember reading something to the effect that in CTP4, any field that is a single key and is int or long is assumed to be an identity column.

Is there any way I can tell EF that this is NOT an identity column? I tried fiddling with the Fluent API, but it's a mystery to me, and there are no data annotations that you can use for this.

Or, alternatively, is there any way I can fiddle with the navigation properties to get this to come 开发者_运维百科out right?


If you're using mapping files with fluent API

this.Property(t => t.Id)
   .HasColumnName("Site_ID")
   .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

I would imagine it should also work declaratively

[HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)]

although I didn't try that.


I discovered I can override the identity behavior with this code:

 modelBuilder.Entity<Visit>().Property(v => v.flaggedDateScheduledDayID).StoreGeneratedPattern = System.Data.Metadata.Edm.StoreGeneratedPattern.None;

However, it is still not making it a foreign key. I guess that's a different question, though. It seems setting the StoreGeneratedPattern to None is the way to override the default behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜