I've searched a lot but failed... I'm trying to create two tables, one having foreign key of another (specifically, Url_Entries 's url to be foreign key of WordCount class). But when I'm adding an object to the databse using code-first I'm getting exception.
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.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
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 input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Enti ty.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
}
}
Exception in the last class. I've commented.
You are setting the ForeignKey
-attribute on the wrong field. You want url
to be the foreign key for Url_Entries
, currently you are setting word
to be the foreign key for url
.
This section is wrong:
public Url_Entries Url_Entries { get; set; }
public string url { get; set; }
[Required, ForeignKey("url")]
public string word { get; set; }
Change to:
[ForeignKey("url")]
public Url_Entries Url_Entries { get; set; }
[Required]
public string url { get; set; }
public string word { get; set; }
To explain the error you are getting in greater detail: You are attempting to set a field of type string
to be the foreign key of a field of type string
. The error is telling you that the property which represents the other entity, referred to as navigation property , must be of "valid entity type". Valid entity type would a complex type which would represent the other table, therefore a string
is not acceptable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.