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.