0

I am newbie to Entity Framework 5. I try to search in the site, there are some similar questions but no answers.

I have an Announcement object and have navigation property Files

public partial class Announcement
    {
        //remove other properties

        public virtual ICollection<File> Files { get; set; }
    }

In sql server there is Announcement table, File table and an AnnouncementFiles table only has 2 fields [AnnouncementId] and [FileId]. The senario is: I create a new Announcement ,but all files has already existing in database. Code is here:

public void CreateAnnouncement(Announcement announcement, List<Guid> fileIds)
        {
            using (var db = new MyDbEntities())
            {
                var files = db.Files.Where(f => fileIds.Contains(f.Id));
                db.Announcements.Add(announcement);
                foreach (var file in files)
                {
                    announcement.Files.Add(file);
                }
                db.SaveChanges();
            }
        }

and there is error like (my .net framework is not English) cannot update EntitySet“AnnouncementFiles”,because it has a DefiningQuery,but do not have the operate to support currnet element.

I cannot change the table structure, so is there any way to do this? some post says use Attach method but announcement.Files do not have this method. Thanks.

2 Answers 2

1

You need to define the Linking table in your DbContext's OnModelCreating method.

You should look up defining many to many relationship in entity framework using FluentApi. The code will look something like this

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);

  modelBuilder.Entity<Annoucement>()
    .HasMany(x => x.Files)
    .WithMany(x => x.Announcements)
    .Map(x => { 
      x.ToTable("AnnouncementFiles"; 
      x.MapLeftKey("AnnouncementID"); 
      x.MapRightKey("FileID"); 
    });
}

The rest of your code is fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I find the problem,that is I need to make both field as primary key in AnnouncementFiles, I dont know why, I do not change any code in my project. Is this the difference between EF5 and before versions?
0

The fileIds.Contains(f.id) cannot be transformed into a sql query. You can construct the expression tree using this link:

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/

1 Comment

Actually EF4 and greater supports Contains().

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.