Reputation: 47
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.
Upvotes: 0
Views: 281
Reputation: 1138
The fileIds.Contains(f.id) cannot be transformed into a sql query. You can construct the expression tree using this link:
Upvotes: 0
Reputation: 4999
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.
Upvotes: 1