首页 / C# / c#-派生类型上的键…但是不是
c#-派生类型上的键…但是不是
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-派生类型上的键…但是不是,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3181字,纯文字阅读大概需要5分钟。
内容图文
我是EF7的新手,并且遇到了一个奇怪的问题.我有这个课:
public class Site
{
public int ID { get; set; }
public string Title { get; set; }
public string HouseNumber { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public DataSource Source { get; set; }
public object Parameters
{
get
{
switch( Source )
{
case DataSource.StealthStats:
return JsonConvert.DeserializeObject<StealthStatsParameters>( JSONParameters );
default:
throw new Exception( "Site::Parameters::get() - Unhandled DataSource " + Source.ToString() );
}
}
set
{
switch( Source )
{
case DataSource.StealthStats:
JSONParameters = JsonConvert.SerializeObject( value );
break;
default:
throw new Exception( "Site::Parameters::set() - Unhandled DataSource " + Source.ToString() );
}
}
}
protected string JSONParameters { get; set; }
public List<Observation> Observations { get; set; }
}
以及上下文的OnModelCreating()中的以下逻辑:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<Site>()
.HasKey( t => t.ID );
builder.Entity<Site>()
.Property( s => s.City )
.IsRequired();
builder.Entity<Site>()
.Property( s => s.HouseNumber )
.IsRequired();
builder.Entity<Site>()
.Property( s => s.Source )
.IsRequired();
builder.Entity<Site>()
.Property( s => s.State )
.IsRequired()
.HasMaxLength( 2 );
builder.Entity<Site>()
.Property( s => s.StreetName )
.IsRequired();
builder.Entity<Site>()
.Property( s => s.Title )
.IsRequired();
builder.Entity<Site>()
.Property( s => s.Zipcode )
.IsRequired()
.HasMaxLength( 10 );
builder.Entity<Observation>()
.HasKey( t => new { t.SiteID, t.TimeStamp } );
builder.Entity<Observation>()
.HasOne( o => o.Site )
.WithMany( s => s.Observations );
}
但是,当我运行dnx ef migrations add时,出现以下错误消息:
The derived type ‘SpeedView.Models.Site’ cannot have keys other than those declared on the root type.
但据我所知,Site并非来自任何东西.
顺便说一句,如果重要的话,这是观察类的定义:
public class Observation
{
public int SiteID { get; set; }
public DateTime TimeStamp { get; set; }
public int MPH { get; set; }
public int VehicleCount { get; set; }
public virtual Site Site { get; set; }
}
顺便说一句,如果有人可以推荐一些指向EF7好的教程和解释的链接,我将不胜感激.在使用EF多年之后,我发现它的学习曲线非常陡峭,而我在网上找到的东西并没有很大帮助.
解决方法:
我将其发布在Entity Framework的github网站上,Smit Patel迅速回答了问题并解释了正在发生的事情.您可以阅读他在https://github.com/aspnet/EntityFramework/issues/5151所写的内容.Thanx,Patel!
简短的版本是这样的:EF类中的对象属性使创建EF迁移的代码将对象类型包括在整个扫描中.由于所有类都是对象的后代,并且对象没有固有的主键,因此所有类都违反“只有根类可以定义键”的限制.
一种解决方案是不将对象属性映射到基础数据库(这就是我所做的).
这样做时,我发现您似乎必须通过将[NotMapped]注释应用于属性来表达您的意图.流利的API方法:
builder.Entity<T>().Ignore(t => t.PropertyToIgnore);
不起作用.
内容总结
以上是互联网集市为您收集整理的c#-派生类型上的键…但是不是全部内容,希望文章能够帮你解决c#-派生类型上的键…但是不是所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。