我搜索了很多,但失败了......我试图创建两个表,一个有另一个外键(具体来说,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.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.在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
}
}
最后一堂课的例外情况。我评论过。
你在滥用'[ForeignKey]' – SLaks
怎么办?我想Url_Entries的URL是WordCount的外键。 –
您发布的错误消息似乎不完整;它在句子的中间结束。 –