2011-10-20 20 views
1

私はこのバグを私のアプリケーションで修正する方法がわかりません。次の関係を持っているデータベースの挿入問題

Customer 
Branch 
PointOfSale 
User 
CustomerEndUser 

::私は、データベースのトランザクションを達成するためにADO.NET Entity Frameworkのを使用しています

Customer->Branch one-to-many 
Branch->PointOfSale one-to-many 
Customer->CustomerEndUser one-to-many 
User->CustomerEndUser one-to-one 
PointOfSale->CustomerEndUser one-to-many 

は私が5つのテーブルを持っています。

新しいCustomerEndUserを追加する場合は、そのユーザーにPointOfSaleとUserとCustomerを付ける必要があります。しかし、CustomerEndUserのInsertメソッドを呼び出すたびに、このメソッドは例外をスローします: "'idenTTsystemEntities.PointOfSaleSet'のエンティティが 'FK_PointsOfSale_Branches'リレーションシップに参加する0関連する 'Branches'が見つかりました1 'Branches'が必要です。私はCustomerEndUserに必要な情報を提供しますが、後でもう一度お試しください。 "ここで

は、私は私のInsertメソッドで使用したコードです:

public static CustomerEndUser InsertIntUser(CustomerEndUser endUser) 
    { 
     idenTTsystemEntities context = new idenTTsystemEntities(); 
     var mbr = Membership.Providers[INTMembershipProviderName]; 
     MembershipCreateStatus userStatus; 

     if(mbr==null) 
     { 
      throw new NullReferenceException("Membership Provider is null"); 
     } 

     MembershipUser mbrUser = mbr.CreateUser(endUser.Username, endUser.Password, endUser.Email, "no question", 
               "no answer", true, Guid.NewGuid(), 
               out userStatus); 

     if(userStatus==MembershipCreateStatus.Success) 
     { 
      ProfileBase profileBase = ProfileBase.Create(endUser.Username); 
      SettingsPropertyValueCollection profileProperties = 
       profileBase.Providers[INTProfileProviderName].GetPropertyValues(profileBase.Context, 
                        ProfileBase.Properties); 

      //add the firstname, lastname and dateofbirth properties 
      profileProperties["FirstName"].PropertyValue = endUser.FirstName; 
      profileProperties["LastName"].PropertyValue = endUser.LastName; 
      var shortDate = new System.Globalization.DateTimeFormatInfo(); 
      shortDate.ShortDatePattern = "dd.MM.yyyy"; 
      profileProperties["DateOfBirth"].PropertyValue = Convert.ToDateTime(endUser.BirthDate, shortDate); 

      //get the id of the newly created end user 
      Guid newUserId = (Guid)mbrUser.ProviderUserKey; 

      //add the user to the CustomerEndUser 
      User newUser = context.UserSet.Where(u => u.UserId == newUserId).First(); 
      endUser.User = newUser; 

      //get the customer to which the end user belongs to 
      Customer cust = context.CustomerSet.Where(c => c.CustomerId == CustomerId).First(); 
      endUser.Customer = cust; 

      //get the pos selected in the insertform 
      Guid posId = endUser.PosId; 
      PointOfSale selectedPos = context.PointOfSaleSet.Include("Branch").Where(br => br.PointOfSaleId == posId).First(); 
      endUser.PointOfSale = selectedPos; 

      endUser.EndUserId = Guid.NewGuid(); 

      context.AddToCustomerEndUsers(endUser); 

      profileProperties["Language"].PropertyValue = "de-DE"; 
      profileBase.Providers[INTProfileProviderName].SetPropertyValues(profileBase.Context, profileProperties); 
      profileBase.Save(); 

      context.SaveChanges(); 

      return endUser; 
     } 

     throw new MembershipCreateUserException(userStatus); 
    } 

Googleは、このストアドプロシージャを使用して固定することができ言いますが、可能であれば私は別のソリューションをしたいと思います。ありがとうございました!

乾杯、 アレックスBarac

答えて

0

Uは、PointsOfSaleが少なくとも1 Branchに関連しなければならないことに気付くべきです。 Customerと同じですが、Branchにも関連する必要があります。

解決策は、PointsOfSaleをロードするときに、関連するブランチもロードする必要があります(顧客と同じです)。 これを行う方法はわかっていますか?

+0

'.INCLUDE( "支店")'。 'selectedPos'の内容をチェックすると、その内部に分岐がありますが、同じ例外がスローされます。例外が顧客に関連しているかどうかはわかりません。 –

0

問題は解決され、挿入方法には関係しませんでした。新しいEndUserを作成するたびに、新しいPointOfSaleを作成してEndUserにアタッチしました。そのため、EntityFrameworkはPointOfSaleも挿入していると考えていました。コンストラクタ内に新しいObjectを作成する代わりに、EntityFrameworkによって作成されたReference.IsLoadedプロパティとReference.EntityKeyプロパティを使用します。

これは、同様の問題を抱えている人にとっては十分な意味があると思います。 'selectedPos' の定義は、まさにそれを行うに

おかげで、

アレックスBarac