2015-04-15 50 views
2

我搜索了很多,但失败了......我试图创建两个表,一个有另一个外键(具体来说,Url_Entries的网址是外键WordCount类)。但是当我使用代码将一个对象添加到数据库时,我首先得到异常。System.InvalidOperationException - CodeFirst

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() at System.Da在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)012.Cntity.Internal.InternalContext.Initialize() 在System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext() at System .Data.Entity.Internal.Linq.InternalSet 1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet 1.在System.Data.Entity.DbSet`1.Add(TEntity entity) at WebCrawler.save(Hashtable hashTable,String url)中添加(对象实体) c: \ Users \ Muhammad Rehan \ Documents \ Visual Studio 2013 \ WebSites \ WebSite7 \ App_Code \ WebCrawler.cs:line 110 at c:\ Users \ Muhammad Rehan \ Documents \ Visual Studio 2013中的WebCrawler.countAndSave(String content,String url) \ WebSites \ WebSite7 \ App_Code \ WebCrawler.cs:line 104 at index.Unnamed_Click(Object sender,EventArgs e)在c:\ Users \ Muhammad Rehan \ Documents \ Visual Studio 2013 \ WebSites \ WebSite7 \ index.aspx.cs:line 19 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web。 UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl ,字符串eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection中POSTDATA) 在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔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

你在滥用'[ForeignKey]' – SLaks

+0

怎么办?我想Url_Entries的URL是WordCount的外键。 –

+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是不可接受的。

+0

现在我得到:System.Data.DataException –

+0

初始化数据库时发生异常。 –

+0

“底层提供程序在打开时失败。” –

相关问题