0

I have two related classes like these :

public partial class WorkItem
{
    public WorkItem()
    {
        this.ChildWorkItems = new HashSet<WorkItem>();
        this.Attachments = new HashSet<Attachment>();
    }

    public int Id { get; set; }
    public Nullable<int> ParentId { get; set; }
    public string Title { get; set; }
    public string SenderUserName { get; set; }

    public virtual ICollection<WorkItem> ChildWorkItems { get; set; }
    public virtual WorkItem ParentWorkItem { get; set; }
    public virtual ICollection<Attachment> Attachments { get; set; }
}

and the realted 1-n attachments :

public partial class Attachment
{
    public int Id { get; set; }
    public string Filename { get; set; }
    public Nullable<int> WorkItemId { get; set; }

    public virtual WorkItem WorkItem { get; set; }
}

Now I want to insert some new workitems with their attachment into the database. I use the following code for insertion :

at first put all workitems (regardless of their attachments )

     var workItems = new List<WorkItem>();
     foreach (var username in AllUsers)
            {
                var workitem = new WorkItem();
                //fill the simple fields

                lst.Add(workitem);
                Context.WorkItems.Add(workitem);
            }

then set the attachments :

        foreach (var fileName in MYFILES)
        {
            var file = new System.IO.FileInfo(fileName);

            foreach (var workItem in workItems)
            {

                var att =
                    new Attachment()
                    {
                        Filename = file.Name,
                    };

                context.Attachments.Add(att);
                att.WorkItem = workItem;

            }
        }

But I get the following exception :

Multiplicity constraint violated. The role 'WorkItem' of the relationship 'AienCRMModel.FK_WorkItemAttachments_WorkItems' has multiplicity 1 or 0..1.

The interesting point is if I only have One workitem , everything is ok. If I have more than one WorkItem with no attachments , again everything is ok. The problem raises when having more than one WorkItem with at least one attachment.

I read lots of other post but nothing usefuk was found.

NOTE : I use EF Code-First 4.3 with a T4 template which generate my classes from the EDMX file.

I Really appreciate your helps in advance.

EDIT I Attached the full EDMX diagram of mentioned tables here : EDMX diagram

EDIT 2

Complete .edmx file at here : http://www.4shared.com/file/zQUO_qk7/CrmModel141.html?

Mahmoud Moravej
  • 8,705
  • 6
  • 46
  • 65
  • The interesting point is that I tried your exact code and it works without any issue so you probably don't show something. – Ladislav Mrnka May 01 '12 at 11:25
  • It works with one WorkItem but not with two or more. Did you test with two or more workItems? – Mahmoud Moravej May 01 '12 at 12:16
  • It works with any # of work items - confirmed - but your code will not connect parentid on its own - you need to have some configuration other than what you're supplying - e.g. fluent code. If properly configured you should have no issues. You should post whatever you have, otherwise we could just post our 'view' at self-referencing entities – NSGaga-mostly-inactive May 01 '12 at 12:19
  • I attached the full EDMX diagram if you think help. I don't have any Fluent API for code-first configuration. As I told I used a T4 template which handles all the db rules through the xml file like what we have in Database/Model first. I can attach the whole .edmx file if you think useful – Mahmoud Moravej May 01 '12 at 12:46
  • it's not made to be mixed together (as I understand you're using T4/EDMX to 'configure' and code-first to create the database) - I can only answer w/ a proper code-first solution for the model. IMO you should 'diverge' from the EDMX dependency (you can still use the existing database). – NSGaga-mostly-inactive May 01 '12 at 17:14
  • Indeed I'm not code-first , just using DbContext instead of ObjectContext. In other words I'm Database-first with DbContext. I will try to bring more details but I don't think the problem comes from EDMX. – Mahmoud Moravej May 01 '12 at 20:06
  • 1
    I think it does Mahmoud (you'd need to 'reference' me like this, @NSGaga for me to get replies). I can post you the proper code-first configuration and all this works w/o any problems whatsoever. You're 'mixing' philosophies - and getting yourself into trouble. Usually there's no help w/ that. – NSGaga-mostly-inactive May 02 '12 at 17:22
  • @NSGaga Thanks for your point :) .As I know DbContext is based on ObjectContext ( a wrapper to support Code-First ). Also I have worked in this model with different complex types for a year and having no problem. Simply and as I told it is not a code-first approach. It is Database-first+DbContext. I think it should not be a 'mixing' (please check this : http://stackoverflow.com/questions/5340990/ado-net-dbcontext-generator-vs-ado-net-poco-entity-generator) . – Mahmoud Moravej May 06 '12 at 10:27
  • @NSGaga I uploaded the whole edmx file (the link is at the bottom of the post) If you think it would be useful :) – Mahmoud Moravej May 06 '12 at 11:03
  • @LadislavMrnka I uploaded the whole edmx file (the link is at the bottom of the post) If you think it would be useful :) – Mahmoud Moravej May 06 '12 at 11:04

1 Answers1

0

Somewhere in your code you are using the same object instance for different rows in the database like this (just an example):

var o1 = new O1Class();
foreach (var o2 in collection)
{
    o2.O1s.Add(o1);
}

But is have to be:

foreach (var o2 in collection)
{
    o2.O1s.Add(new O1Class());
}

I this example O1Class can have only one O2Class in their relationship but the Entity Framework keep the reference on the o1 instance so when you adding it to multiple O2Class instances the EF will think that the current O1Class has multiple O2Class instances which is incorrect.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38