views:

49

answers:

1

I have two objects... and if I compile a program with either one, it works fine, but when they both exist in the same program, I get the exception...

*"Entities in 'ObjectContext.UnitSet' participate in the 'Sheet_Statistics' relationship. 0 related 'Sheet' were found. 1 'Sheet' is expected."*

class Unit
{
 public int Id;
 public string Name;
}
class Template
{
 public int Id;
 public virtual ICollection<Unit> Units
}

class Sheet
{
 public int Id;
 public virtual ICollection<Unit> Units
}

Then their configurations..

TemplateConfiguration : EntityConfiguration // ....

        //// map the collection entity
        HasMany(k => k.Units).WithRequired()
            .Map("template.units",
                (template, unit) => new
                {
                    Template = template.Id,
                    Unit = unit.Id
                });

SheetConfiguration : EntityConfiguration // ....

        //// map the collection entity
        HasMany(k => k.Units).WithRequired()
            .Map("sheet.units",
                (sheet, unit) => new
                {
                    Sheet = sheet.Id,
                    Unit = unit.Id
                });

UnitConfiguration : EntityConfiguration<Unit>
// 
            // Initialize the Primary Key
            HasKey(k => k.Id);
            // Initialize that the Key Increments as an Identity
            Property(k => k.Id).IsIdentity();


var templates = new List<Template>
                {
                    new Template
                    {
                        Name = // ..,
                        Units = new List<Unit>
                        {
                            new Unit
                            {
                                // ...
                            }
                        }
                    }
                };
                templates.ForEach(x =>
                {
                    context.Templates.Add(x);
                });
                context.SaveChanges(); // <-- Exception Happens Here, I never even get to try to add Sheets.
+2  A: 

I'm taking a stab at this because without seeing all your code, I can't solve much more of it. I think your problem is that you're creating Units but not setting some sort of Sheet property (you need to provide all your entity/config code). You need to create the Sheet and Unit both before you can save the Unit or Sheet since they have a required reference (hence the error you're getting). If you provide more code I'll be able to refine my answer better.

TheCloudlessSky
Your theory sounds good, I have updated the code. There literally isn't anything but more closing/opening brackets and specific strings to add that have anything to do with this code.
Stacey
@Stacey - I think you need to make sure that your DB schema matches what you're trying to map. Somewhere along the line things aren't matching up. For example, what is `Sheet_Statistics`?
TheCloudlessSky
There is nothing called Sheet_Statistics! That's the problem. I wasn't even making sheets. However I am going to try changing WithRequired() to WithOptional() and see if I can get different results.
Stacey
@Stacey - Why don't you try creating `Unit`s *outside* of the template? Add them to the `Units` collection (or whatever) and *then* add them to the template units. It might be because they're not added to the `Context` yet.
TheCloudlessSky
@Stacey - Check out this post for checking the state of the Entities when they're added to the context when debugging. http://stackoverflow.com/questions/1290287/entity-framework-wont-savechanges-on-new-entity-with-two-level-relationship/2338507#2338507
TheCloudlessSky
You were correct. It was the dependency on Sheet.
Stacey