0

Is this code correct to add a record with a relationship Id? If i enter the Id directly, i get the following error

Cannot implicitly convert type 'int' to 'Project.Models.Database.ArticleDatabase'

Is this code below the correct way?

var newArticle = new Articles();

newArticle.Artname = addarticle.Articlename;

//NOT WORKING
// newArticle.Artdatabase = addarticle.ArticlegroupId

//WORKING, SEEMS WRONG
newArticle.Artdatabase = _context.ArticleDatabase.SingleOrDefault(c => c.Id == addarticle.ArticlegroupId);


newArticle.Articleaadddate = DateTime.Now;
newArticle.Articlelastedit = DateTime.Now;
newArticle.Artstatus = 3;

_context.Articles.Add(newArticle);
_context.SaveChanges();
5
  • 1
    Do you not have a navigation property to the ArticleGroup? Because then you could just get the group you need as an entity and set it on the newArticle.ArticleGroup property and EF will handle the rest. Commented Sep 9, 2016 at 11:45
  • The code you have marked as WORKING looks fine. The Artdatabase property is of type "ArticleDatabase" so you can only assign something that is assignable to "ArticleDatabase" - you first try to set it to an Int (which didn't work), but then finding a reference to an actual instance of the object did work... what seems wrong about that? Commented Sep 9, 2016 at 11:47
  • @Webruster he says that with the not working code he gets the exception stating that the type is Project.Models.Database.ArticleDatabase Commented Sep 9, 2016 at 11:57
  • @ScottPerham - I don't believe it's an exception, but rather a compile time error. Commented Sep 9, 2016 at 12:28
  • 1
    You are right, I didn't really mean exception to be honest... I meant "error" :) Commented Sep 9, 2016 at 12:51

1 Answer 1

3

//NOT WORKING

// newArticle.Artdatabase = addarticle.ArticlegroupId

That's because you're trying to set the ArticlegroupId (which seems to be an int) to a property that seems to be of type ArticleDatabase.

I assume your model looks something like this:

public class Articles
{
    public string Artname { get; set; }

    [ForeignKey("ArtdatabaseId")]
    public virtual ArticleDataBase Artdatabase { get; set; }

    // For alternative 2 you'll need this.
    public int ArtdatabaseId { get; set; }
}

Alternative 1. Use the navigation property. When using the navigation property you need to attach it.

var newArticle = new Articles();

newArticle.Artname = addarticle.Articlename;

// Create the entity and attach it.
var artDataBase = new ArticleDataBase
{
    Id = addarticle.ArticlegroupId
};
_context.ArticleDatabase.Attach(artDataBase);

newArticle.Artdatabase = artDataBase;
newArticle.Articleaadddate = DateTime.Now;
newArticle.Articlelastedit = DateTime.Now;
newArticle.Artstatus = 3;

_context.Articles.Add(newArticle);
_context.SaveChanges();

Alternative 2. Use the foreign key-property. When using this option you need to explicitly define the foreign key in your model.

var newArticle = new Articles();

newArticle.Artname = addarticle.Articlename;
// Note that I'm using ArtdatabaseId and not Artdatabase.
newArticle.ArtdatabaseId = addarticle.ArticlegroupId

newArticle.Articleaadddate = DateTime.Now;
newArticle.Articlelastedit = DateTime.Now;
newArticle.Artstatus = 3;

_context.Articles.Add(newArticle);
_context.SaveChanges();

Take a look at this article for defining foreign keys with fluent instead.

modelBuilder.Entity<Post>().HasRequired(p => p.Blog) 
                .WithMany(b => b.Posts) 
                .HasForeignKey(p => p.FKBlogId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.