我经常搜索但失败了......我试图创建两个表,一个表有另一个表的外键(具体来说, Url_Entries ' url成为 WordCount 类的外键。但是当我使用代码优先向数据库添加一个对象时,我得到了异常。
System.InvalidOperationException未被用户代码处理 的HResult = -2146233079 消息=属性' url'无法配置为导航属性。该属性必须是有效的实体类型,并且该属性应具有非抽象的getter和setter。对于集合属性,类型必须实现ICollection,其中T是有效的实体类型。 来源=的EntityFramework 堆栈跟踪: 在System.Data.Entity.ModelConfiguration.Configuration.ConventionTypeConfiguration.NavigationProperty(PropertyPath propertyPath) 在System.Data.Entity.ModelConfiguration.Configuration.ConventionTypeConfiguration.NavigationProperty(PropertyInfo propertyInfo) 在System.Data.Entity.ModelConfiguration.Conventions.ForeignKeyPrimitivePropertyAttributeConvention.Apply(PropertyInfo memberInfo,ConventionTypeConfiguration配置,ForeignKeyAttribute属性) 在System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention
1.<.ctor>b__0(ConventionTypeConfiguration ec) at System.Data.Entity.ModelConfiguration.Conventions.TypeConvention.ApplyCore(Type memberInfo, ModelConfiguration modelConfiguration) at System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(Type memberInfo, ModelConfiguration modelConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration) at System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration) at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type) at System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model) at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy
2.GetValue(TInput输入) 在System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 在System.Data.Entity.Internal.InternalContext.Initialize() 在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 在System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet
1.Add(Object entity) 在System.Data.Entity.DbSet`1.Add(TEntity实体) 在WebCrawler.save(Hashtable hashTable,String url)中的c:\ Users \ Muhammad Rehan \ Documents \ Visual Studio 2013 \ WebSites \ WebSite7 \ App_Code \ WebCrawler.cs:第110行 在C:\ Users \ Muhammad Rehan \ Documents \ Visual Studio 2013 \ WebSites \ WebSite7 \ App_Code \ WebCrawler.cs中的WebCrawler.countAndSave(String content,String url):第104行 at index.Unnamed_Click(Object sender,EventArgs e)位于c:\ Users \ Muhammad Rehan \ Documents \ Visual Studio 2013 \ WebSites \ WebSite7 \ index.aspx.cs:第19行 在System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 在System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) InnerException:
public class Url_Entries
{
public Url_Entries(string url)
{
this.Url = url;
}
[Key]
public string Url { get; set; }
[Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime date { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int crawl_id { get; set; }
}
public class WordCount
{
public WordCount(string url, string word, int count)
{
this.url = url;
this.word = word;
this.count = count;
}
public Url_Entries Url_Entries { get; set; }
public string url { get; set; }
[Required, ForeignKey("url")]
public string word { get; set; }
public int count { get; set; }
}
public class MyDbContext:DbContext
{
public MyDbContext()
: base("ExploreCalifornia")
{
}
public DbSet<Url_Entries> Url_Entries { get; set; }
public DbSet<WordCount> WordCount { get; set; }
}
public class WebCrawler
{
public static MyDbContext db = new MyDbContext();
private static void save(Hashtable hashTable, string url)
{
Url_Entries newUrlEntry = new Url_Entries(url);
db.Url_Entries.Add(newUrlEntry); //Exception on this line
}
}
上一课中的例外情况。我发表了评论。
答案 0 :(得分:0)
您在错误字段上设置ForeignKey
- 属性。您希望url
成为Url_Entries
的外键,目前您将word
设置为url
的外键。
这部分错了:
public Url_Entries Url_Entries { get; set; }
public string url { get; set; }
[Required, ForeignKey("url")]
public string word { get; set; }
更改为:
[ForeignKey("url")]
public Url_Entries Url_Entries { get; set; }
[Required]
public string url { get; set; }
public string word { get; set; }
要更详细地解释您正在获得的错误:您正在尝试将类型为string
的字段设置为string
类型字段的外键。该错误告诉您,表示另一个实体的属性(称为导航属性)必须是“有效实体类型”。有效的实体类型是一个复杂的类型,它代表另一个表,因此string
是不可接受的。