2

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?

asked Sep 6, 2017 at 15:39
3
  • Try it out, see if it works! Commented 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. Commented Sep 6, 2017 at 15:45
  • Thanks, I tried but it was not successfull :( Commented Sep 6, 2017 at 15:52

2 Answers 2

1

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);
 }
}
answered Sep 6, 2017 at 16:01
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that with no luck. But I guess I also have to create the other related classes as code first models. Ex. screencast.com/t/CSXxu2DKW
Yes, this works. But I would like to include the categories too.
0

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.

answered Sep 6, 2017 at 15:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.