我有三个表,player
,player points
和team
。我有一个外键设置为团队和玩家点,但当我尝试插入播放器表时,我收到以下错误。
try
{
string id = Request.QueryString["id"];
if (id != "00000000-0000-0000-0000-000000000000")
{
_playerId = string.IsNullOrEmpty(id) ? new Guid() : new Guid(id);
}
string teamIds = Request.QueryString["teamId"];
if (teamIds != "00000000-0000-0000-0000-000000000000")
{
_teamId = string.IsNullOrEmpty(id) ? new Guid() : new Guid(id);
}
player _player = new player();
_player = _dal.GetPlayerBYID(_playerId);
_player.Name = txtFullName.Text;
_player.email = txtEmail.Text;
_player.address = txtAddress.Text;
_player.description = txtDescription.InnerText;
_player.gender = dlGenders.SelectedValue.ToString();
_player.regEmailSent = chksendActivationEmail.Checked;
_player.modifiedDate = DateTime.Now;
_player.teamId = new Guid(ddlTeam.SelectedValue.ToString());
player_points _points = new player_points();
if (Convert.ToDateTime(dateOfBirth.Text) == new DateTime(1900, 1, 1))
_player.dob = Convert.ToDateTime(dateOfBirth.Text);
if (_player.player_id == null)
_player.player_id = _dal.genPlayerID(new Guid("B9B2A89A-2295-4E48-BBA4-9DC28A1855FB"), new Guid("6E424913-A9A4-4A46-9785-C8AFA09C82DB"));
if (_teamId != Guid.Empty)
{
_player.teamId = _teamId;
}
if (id == "00000000-0000-0000-0000-000000000000")
{
_dal.SoccerEntities.AddToplayers(_player);
}
_dal.SoccerEntities.SaveChanges();
}
我的问题是:如何从保存中获取id值,以便能够链接我的播放器点数表以获取错误:
INSERT语句与FOREIGN KEY约束“FK_player_points”冲突。冲突发生在数据库\“DB_9DF962_davidbuckleyni37 \”,table \“dbo.player_points \”,列'id'。\ r \ n语句已终止。
显然,在添加到玩家点数表之前,我不需要在SQL Server端使用newid()
填充的玩家身份证号码。
此屏幕截图显示了我的播放器表:
这个显示我的玩家积分表:
外键是基于玩家ID的,所以我认为我必须在玩家积分中记录保存工作,但是当我没有保存id grrr时,如何获得玩家ID
现在玩家可以有很多玩家积分值,所以这是我设置的正确方法
当我尝试使用marc方法时,由于某种原因我没有添加选项,为什么会这样?。
marc的解决方案不会起作用,因为我似乎没有并在我的玩家点上添加功能。
答案 0 :(得分:2)
基本上 - 不要搞砸这个 - 让EF处理所有细节!
您只需创建播放器
player _player = new player();
// ... set all the _player properties
然后你的玩家指出:
player_points _points = new player_points();
// set the properties of _points as needed
然后您将_points
分配到_player
到其导航属性中(我不知道它叫什么 - 您没有向我们展示) :
// this navigation property might be called something else! Adapt as needed!
_player.player_points.Add(_points);
然后你保存两个,在一次调用中链接新对象,让EF发挥其魔力:
_dal.SoccerEntities.AddToplayers(_player);
_dal.SoccerEntities.SaveChanges();
完成了! EF将插入两个实体,它将修复player_points
和player
表中的条目之间的关系 - 所有这些都没有您干预。不要让你的开发人员过于艰难!使用EF的魔力!
更新:我仍然不知道您player_points
课程中的player
数据类型是什么.....我假设它会是集合(因此我使用.Add()
方法),单个玩家可以有多个玩家点。
如果那个不的情况 - 如果这两个实体以1:1的方式链接(一个玩家只有一个player_points),那么你必须请改用此代码:
_player.player_points = _points;
(而不是使用.Add()
方法调用)然后继续到最后
答案 1 :(得分:0)
对于任何其他人而言,这是我必须在实际积分表上添加我的外键而不是玩家表我没有学校男孩错误,但感谢所有人的帮助。
CREATE TABLE [dbo].[player_points](
[id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_player_points_id] DEFAULT (newid()),
[playerId] [uniqueidentifier] NOT NULL,
[playerName] [nvarchar](255) NULL,
[teamId] [uniqueidentifier] NULL,
[points] [int] NULL,
[pointsToRedeem] [int] NULL,
[dateToBeAwarded] [date] NULL,
[approvedBy] [varchar](50) NULL,
[approved] [bit] NULL,
[approvedDate] [date] NULL,
[siteId] [uniqueidentifier] NULL,
[sentApproveEmail] [bit] NULL,
[isActivated] [bit] NULL,
[authUserName] [nvarchar](50) NULL,
[authCreatedDate] [date] NULL,
[modifiedDate] [date] NULL,
[modifiedBy] [nvarchar](255) NULL,
[playerNumber] [nvarchar](50) NULL,
[isProcessed] [bit] NULL,
[sentEmailToAdmin] [bit] NULL,
[sentEmailToUser] [bit] NULL,
CONSTRAINT [PK_player_points] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[player_points] WITH CHECK ADD CONSTRAINT [FK_PlayerPoints] FOREIGN KEY([playerId])
REFERENCES [dbo].[player] ([id])
GO
ALTER TABLE [dbo].[player_points] CHECK CONSTRAINT [FK_PlayerPoints]
GO