gpt4 book ai didi

c# - System.InvalidOperationException - CodeFirst

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:41 26 4
gpt4 key购买 nike

我搜索了很多但都失败了...我正在尝试创建两个表,一个具有另一个的外键(具体来说,Url_Entries 的 url 是 WordCount 类)。但是当我使用代码优先将对象添加到数据库时,我遇到了异常。

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=The property 'url' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type. Source=EntityFramework StackTrace: at System.Data.Entity.ModelConfiguration.Configuration.ConventionTypeConfiguration.NavigationProperty(PropertyPath propertyPath) at System.Data.Entity.ModelConfiguration.Configuration.ConventionTypeConfiguration.NavigationProperty(PropertyInfo propertyInfo) at System.Data.Entity.ModelConfiguration.Conventions.ForeignKeyPrimitivePropertyAttributeConvention.Apply(PropertyInfo memberInfo, ConventionTypeConfiguration configuration, ForeignKeyAttribute attribute) at System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention1.<.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 input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at 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) at System.Data.Entity.DbSet`1.Add(TEntity entity) at WebCrawler.save(Hashtable hashTable, String url) in c:\Users\Muhammad Rehan\Documents\Visual Studio 2013\WebSites\WebSite7\App_Code\WebCrawler.cs:line 110 at WebCrawler.countAndSave(String content, String url) in c:\Users\Muhammad Rehan\Documents\Visual Studio 2013\WebSites\WebSite7\App_Code\WebCrawler.cs:line 104 at index.Unnamed_Click(Object sender, EventArgs e) in 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, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at 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
}
}

最后一节课异常。我评论过了。

最佳答案

您在错误的字段上设置了 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 是 Not Acceptable 。

关于c# - System.InvalidOperationException - CodeFirst,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com