MVC4创建论坛帖子并同时发帖外键异常错误

时间:2014-01-23 15:15:29

标签: asp.net-mvc-4

我还在努力创建论坛。我正在尝试同时用帖子创建新主题(就像每个普通的论坛一样)。我有两个模型:

FORUMTHREAD

public partial class FORUMTHREAD
{
    public FORUMTHREAD()
    {
        this.FORUMCATEGORY = new HashSet<FORUMCATEGORY>();
        this.FORUMPOST = new HashSet<FORUMPOST>();
    }

    public int TH_ID { get; set; }
    public System.DateTime DATE { get; set; }
    public string TOPIC { get; set; }
    public string USER { get; set; }

    public virtual ICollection<FORUMCATEGORY> FORUMCATEGORY { get; set; }
    public virtual ICollection<FORUMPOST> FORUMPOST { get; set; }
}

FORUMPOST

public partial class FORUMPOST
{
    public FORUMPOST()
    {
        this.FORUMTHREAD = new HashSet<FORUMTHREAD>();
    }

    public int PO_ID { get; set; }
    public System.DateTime DATE { get; set; }
    public string POST { get; set; }
    public string USER { get; set; }

    public virtual ICollection<FORUMTHREAD> FORUMTHREAD { get; set; }
}

在ForumController创建功能中,我添加了这个:

[Authorize(Roles = "Administrator,Moderator,User")]
    public ActionResult Create()
    {
        var db = new MainDatabaseEntities();

        var viewModel = new FORUMTHREAD
        {
            FORUMCATEGORY = db.FORUMCATEGORY.Select(c => new { CA_ID = c.CA_ID, CATEGORY = c.CATEGORY, isSelected = false }).ToList().Select(g => new FORUMCATEGORY
            {
                CA_ID = g.CA_ID,
                CATEGORY = g.CATEGORY,
                isSelected = false
            }).ToList(),
        };


        return View(viewModel);
    }

    //
    // POST: /Forum/Create
    [Authorize(Roles = "Administrator,Moderator,User")]
    [HttpPost]
    public ActionResult Create(FORUMTHREAD forumthread,FORUMPOST forumpost, String user, String post)
    {
        var db = new MainDatabaseEntities();

        var newthread = new FORUMTHREAD
        {
            TH_ID = forumthread.TH_ID,
            DATE = DateTime.Now,
            TOPIC = forumthread.TOPIC,
            USER = user,
            FORUMCATEGORY = new List<FORUMCATEGORY>(),
            FORUMPOST = new List<FORUMPOST>(),
        };
        var newpost = new FORUMPOST
        {
            PO_ID = forumpost.PO_ID,
            POST = post,
            USER = user,
            DATE = DateTime.Now, 
            FORUMTHREAD = forumpost.FORUMTHREAD
        };
        foreach (var selectedThread in forumthread.FORUMCATEGORY.Where(c => c.isSelected))
        {
            var category = new FORUMCATEGORY { CA_ID = selectedThread.CA_ID };
            db.FORUMCATEGORY.Attach(category);
            newthread.FORUMCATEGORY.Add(category);
        }

        db.FORUMTHREAD.Add(newthread);
        newthread.FORUMPOST.Add(newpost);
        db.FORUMPOST.Attach(newpost);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

观看:

@model AnimeWeb.Models.FORUMTHREAD

<h2>New Thread</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <div class="editor-field">
        @Html.HiddenFor(model => model.TH_ID)
        @Html.ValidationMessageFor(model => model.TH_ID)
    </div>
    <div class="editor-label">
        Topic
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.TOPIC)
        @Html.ValidationMessageFor(model => model.TOPIC)
    </div>
    <div class="editor-label">
        Tags
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FORUMCATEGORY)
        @Html.ValidationMessageFor(model => model.FORUMCATEGORY)
    </div>
    <div>
        Post
    </div>
    <div>
        <input type="text" id="ss" name="post" value="" />
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

当我尝试保存新主题时,我收到错误:

{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_TH_PO_ToTable_1\". The conflict occurred in database \"C:\\USERS\\PLACEK\\DOCUMENTS\\VISUAL STUDIO 2013\\PROJECTS\\ANIMEWEB\\ANIMEWEB\\APP_DATA\\MAINDATABASE.MDF\", table \"dbo.FORUMPOST\", column 'PO_ID'.\r\nThe statement has been terminated."}

有人可以帮我写一个有用的创建功能吗?

编辑:

SQL中的表定义:

CREATE TABLE [dbo].[FORUMTHREAD] (
[TH_ID] INT            IDENTITY (1, 1) NOT NULL,
[DATE]  DATETIME       NOT NULL,
[TOPIC] NVARCHAR (MAX) NOT NULL,
[USER]  NVARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([TH_ID] ASC)
);

CREATE TABLE [dbo].[FORUMPOST] (
[PO_ID] INT            IDENTITY (1, 1) NOT NULL,
[DATE]  DATETIME       NOT NULL,
[POST]  NVARCHAR (MAX) NOT NULL,
[USER]  NVARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([PO_ID] ASC)
);

CREATE TABLE [dbo].[TH_PO] (
[TH_ID] INT NOT NULL,
[PO_ID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([TH_ID] ASC, [PO_ID] ASC),
CONSTRAINT [FK_TH_PO_ToTable_1] FOREIGN KEY ([PO_ID]) REFERENCES [dbo].[FORUMPOST] ([PO_ID]),
CONSTRAINT [FK_TH_PO_ToTable] FOREIGN KEY ([TH_ID]) REFERENCES [dbo].[FORUMTHREAD] ([TH_ID])
);

0 个答案:

没有答案