Know the title is a bit vague and not very descriptive for my real question, but here goes.
I have a new ASP MVC project, where I have EF setup. It is working like a charm. Now I would like to create a new code first model which I'm going to use to store RSS feed items.
Instead of creating the model from scratch, I would very much like to use System.ServiceModel.Syndication.SyndicationItem
as a basis for my model. I might want to add some custom fields, but for now I can pretty much use the exact same fields and field types.
My question then is, can I inherit from this class into my code first model? or do I have to make the exact same fields and field types double up?
-
Try it out, see if it works!Steven Ackley– Steven Ackley2017年09月06日 15:41:01 +00:00Commented Sep 6, 2017 at 15:41
-
I don't see any reason why this would not work, EF usually is not too constraining towards the entity types used in the model.DevilSuichiro– DevilSuichiro2017年09月06日 15:45:31 +00:00Commented Sep 6, 2017 at 15:45
-
Thanks, I tried but it was not successfull :(Martin at Mennt– Martin at Mennt2017年09月06日 15:52:46 +00:00Commented Sep 6, 2017 at 15:52
2 Answers 2
If it's only SyndicationItem and SyndicationCategory that you're after and don't care about ignoring some properties, you can try the following:
public class FeedItem : SyndicationItem
{
[Key]
public int Id { get; set; }
// additional properties go here
}
public class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
}
public DbSet<FeedItem> FeedItems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<FeedItem>().HasKey<int>(f => f.Id);
modelBuilder.Entity<SyndicationCategory>().HasKey(c => c.Name);
// Ignore entities
modelBuilder.Ignore<SyndicationPerson>();
modelBuilder.Ignore<SyndicationLink>();
modelBuilder.Ignore<SyndicationContent>();
modelBuilder.Ignore<SyndicationElementExtension>();
// Ignore properties
modelBuilder.Entity<FeedItem>().Ignore(f => f.AttributeExtensions);
}
}
2 Comments
Okay, I tried this.
Model
namespace App.Models
{
public class FeedItem : System.ServiceModel.Syndication.SyndicationItem
{
}
}
DB Context
namespace App.DAL
{
public partial class DbContext : IdentityDbContext<ApplicationUser>
{
public DbContext() : base("name=DbContext")
{
}
public static DbContext Create()
{
return new DbContext();
}
public DbSet<FeedItem> FeedItems { get; set; }
}
}
And got this in return when running the app.
App.DAL.SyndicationPerson: : EntityType 'SyndicationPerson' has no key defined. Define the key for this EntityType. App.DAL.SyndicationElementExtension: : EntityType 'SyndicationElementExtension' has no key defined. Define the key for this EntityType. App.DAL.SyndicationCategory: : EntityType 'SyndicationCategory' has no key defined. Define the key for this EntityType. App.DAL.SyndicationLink: : EntityType 'SyndicationLink' has no key defined. Define the key for this EntityType. SyndicationContents: : The referenced EntitySet 'SyndicationContents' for End 'FeedItem_Copyright_Target' could not be found in the containing EntityContainer. SyndicationContents: : The referenced EntitySet 'SyndicationContents' for End 'SyndicationFeed_Copyright_Target' could not be found in the containing EntityContainer. SyndicationContents: : The referenced EntitySet 'SyndicationContents' for End 'SyndicationFeed_Description_Target' could not be found in the containing EntityContainer. SyndicationContents: : The referenced EntitySet 'SyndicationContents' for End 'SyndicationFeed_Title_Target' could not be found in the containing EntityContainer. SyndicationContents: : The referenced EntitySet 'SyndicationContents' for End 'FeedItem_Summary_Target' could not be found in the containing EntityContainer. SyndicationContents: : The referenced EntitySet 'SyndicationContents' for End 'FeedItem_Title_Target' could not be found in the containing EntityContainer. SyndicationPersons: EntityType: EntitySet 'SyndicationPersons' is based on type 'SyndicationPerson' that has no keys defined. SyndicationElementExtensions: EntityType: EntitySet 'SyndicationElementExtensions' is based on type 'SyndicationElementExtension' that has no keys defined. SyndicationCategories: EntityType: EntitySet 'SyndicationCategories' is based on type 'SyndicationCategory' that has no keys defined. SyndicationLinks: EntityType: EntitySet 'SyndicationLinks' is based on type 'SyndicationLink' that has no keys defined. SyndicationContents: EntityType: EntitySet 'SyndicationContents' is based on type 'SyndicationContent' that has no keys defined.
I might have missed something.
Comments
Explore related questions
See similar questions with these tags.