实体框架不更新我的表值

时间:2013-03-27 08:13:48

标签: asp.net-mvc-3 entity-framework

我不明白,我的model.edmx有两张表:

enter image description here

然后我有了3张桌子:

CREATE TABLE [Group].[RecommendedUserGroups]
(
 [Groups_GroupId] INT NOT NULL,
 [RecommendedUsers_RecommendedUserId] int NOT NULL,
 CONSTRAINT [PK_GroupRecommendedUser] PRIMARY KEY CLUSTERED ([Groups_GroupId] ASC, [RecommendedUsers_RecommendedUserId] ASC),
 CONSTRAINT [FK_RecommendedUsers_RecommendedUserGroups] FOREIGN KEY ([RecommendedUsers_RecommendedUserId]) REFERENCES [Zinc].[RecommendedUsers] ([RecommendedUserId]),
 CONSTRAINT [FK_Groups_RecommendedUserGroups] FOREIGN KEY ([Groups_GroupId]) REFERENCES [Group].[Groups] ([GroupId])
 )

CREATE TABLE [Group].[Groups]
(
 [GroupId] INT NOT NULL IDENTITY(1, 1), 
 [Customers_CustomerId] INT NOT NULL,
 [Name] NVARCHAR(128) NOT NULL, 
 [IsArchived] BIT NOT NULL,
 [SmallImageUrl]        NVARCHAR (256) NULL,
 [MediumImageUrl]       NVARCHAR (256) NULL,
 [LargeImageUrl]        NVARCHAR (256) NULL,
 [AllowFiltering] BIT NOT NULL DEFAULT 0, 
  [IsSegment] BIT NOT NULL CONSTRAINT DF_Groups_IsSegment DEFAULT 0,
  CONSTRAINT [PK_Groups] PRIMARY KEY CLUSTERED ([GroupId] ASC), 
  CONSTRAINT [FK_Groups_Customers] FOREIGN KEY ([Customers_CustomerId]) REFERENCES [Zinc].[Customers] ([CustomerId]),      
 )

CREATE TABLE [Zinc].[RecommendedUsers]
(
 [RecommendedUserId]                 INT IDENTITY(1,1) NOT NULL,             
 [Firstname]                         NVARCHAR (50)    NOT NULL,
 [Surname]                           NVARCHAR (50)    NOT NULL,
 [Email]                             NVARCHAR (256)   NOT NULL,
 [Department]                        NVARCHAR (256)   NULL,
 [JobTitle]                          NVARCHAR (256)   NULL, 
 [DateAndTimeProcessed]              DATETIME         NULL,
 [Users_UserId]                      INT              NOT NULL, 
 [DateAndTimeRecommended]            DATETIME         NOT NULL,
 [AssignedUserId]                    INT              NULL,  
 CONSTRAINT [PK_RecommendedUsers] PRIMARY KEY CLUSTERED ([RecommendedUserId] ASC),
)

然后我的实体:

namespace Zinc.Entities
{
 public class RecommendedUser
 {
  #region Properties

 public virtual int RecommendedUserId { get; set; }
 public virtual string Firstname { get; set; }
 public virtual string Surname { get; set; }
 public virtual string Email { get; set; }
 public virtual string Department { get; set; }
 public virtual string JobTitle { get; set; }
 public virtual DateTime? DateAndTimeProcessed { get; set; }
 public virtual int Users_UserId { get; set; }
 public virtual DateTime? DateAndTimeRecommended { get; set; }
 public virtual int AssignedUserId { get; set; }
 public virtual ICollection<Group.Group> Groups { get; set; }

#endregion

 }
}

namespace Zinc.Entities.Group

{   公共班组   {     #region属性

public virtual int GroupId { get; set; }
public virtual string Name { get; set; }
public virtual bool IsArchived { get; set; }
public virtual bool IsSegment { get; set; }
public virtual bool AllowFiltering { get; set; }

public virtual string SmallImageUrl { get; set; }
public virtual string MediumImageUrl { get; set; }
public virtual string LargeImageUrl { get; set; }

public virtual Entities.Customer Customer { get; set; }

#endregion

#region ICollections

 //i am only concerned with recommended user so will just put this collection here


  private ICollection<RecommendedUser> _groupRecommendedUsers;
public virtual ICollection<RecommendedUser> RecommendedUsers
{
  get
  {
    if (_groupRecommendedUsers == null)
      _groupRecommendedUsers = new List<RecommendedUser>();

    return _groupRecommendedUsers;
  }
  set
  {
    _groupRecommendedUsers = value;
  }
}

所以当我使用推荐用户详细信息保存组时,它会写入我的推荐用户表但不更新我的RecommendedUserGroups表吗?

有人可以帮忙吗?我用的是mvc3

1 个答案:

答案 0 :(得分:1)

您的RecommendedUserGroups表的主键应该是表中的两个字段。

定义表的方式每个组只能有一个成员。