1

I am using Entity Framework with fluent mapping.

In my application I want to fetch data from a view which is not having a key.

I have written a entity mapping for fluent api and a method to fetch data from view

When I tried to test the method, I get an error:

Entity Type has no key defined, Define a key for this entity type

Can I fetch data from view with out a key using Entity Framework with fluent. below is the entity class

public class WeatherData : BaseModel
{
    public DateTime ReportDateTime { get; set; }
    public string VendorName { get; set; }
    public string WeatherTypeCd { get; set; }
    public string WeatherStationCd { get; set; }
    public DateTime WeatherDate { get; set; }
    public int Interval { get; set; }
    public string IntervalTypeCd { get; set; }
    public string WeatherValueTypeCd { get; set; }
    public decimal ValueMax { get; set; }
    public decimal ValueMin { get; set; }
    public decimal ValueAvg { get; set; }
    public string Unit { get; set; }
    public int DegreeDayBase { get; set; }
    public string UpdateUser { get; set; }
    public DateTime UpdateDt { get; set; }
    public int VendorId { get; set; }
    public int WeatherStationId { get; set; }
    public int WeatherTypeId { get; set; }
    public int IntervalTypeId { get; set; }
    public int WeatherValueTypeId { get; set; }
}

This is the mapping class

public class WeatherDataMap : EntityTypeConfiguration<WeatherData>
{
    public WeatherDataMap() : base()
    {
        this.ToTable("dbo.vw_WeatherData");
    }
}

I cannot change the view, I only have read permissions.

4
  • 1
    Define a key for this entity type. Pretty clear, isn't it? If you need more help post your code. Dec 27, 2017 at 10:55
  • Voting to close this because there's no code.
    – DavidG
    Dec 27, 2017 at 10:56
  • There is no difference between Table or View for EF you can decorate one unique prop by [Key], anyway Possible duplicated of stackoverflow.com/questions/20203492/…
    – Aria
    Dec 27, 2017 at 11:03
  • @deepak find a combination of properties that are likely to be unique and declare that as a composite key in EF. To ensure EF doesn't complain much, use .AsNoTracking() extension.
    – zaitsman
    Dec 27, 2017 at 11:24

1 Answer 1

2

There is no problem if your table or view does not have primary key, but to avoid that mentioned exception your model must have PK so as the EF-Code First documentation says you can define Key a PK on more then one column or on one column so your model would be like :

public partial class WeatherData : BaseModel
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int VendorId { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int WeatherStationId { get; set; }

    [Key]
    [Column(Order = 2)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int IntervalTypeId { get; set; }

    [StringLength(50)]
    public string VendorName { get; set; }

    [StringLength(50)]
    public string WeatherTypeCd { get; set; }
}

To use Find method of a DbSet you must take this order for the key parameters.

But please note if you generate EF-Code first model by POCO or Wizard the EF define all the int columns as the primary key as you can see in the above model, but you can define one prop as key but you should be sure there is no duplicated field which you defined as the key otherwise you should combine multiple columns as the primary key...

The other way is use Fluent Api to define key as the below code in OnModelCreating of your context:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<WeatherData>().Property(a => a.VendorId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<WeatherData>().Property(a => a.WeatherStationId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<WeatherData>().HasKey(w => new
        {
            w.VendorId,
            w.WeatherStationId
        });
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.