ASP.NET MVC CRUD与LocalDb savechanges错误

时间:2017-02-02 02:23:28

标签: c# asp.net-mvc localdb

我正在使用VS 2015,我创建了一个ASP.NET MVC项目,并添加了一些功能(登录和注册确实可以正常使用名为MyDatabase.mdf的Localdb)。

CRUD只在db中创建函数,其余部分将在稍后出现。

但现在我无法完成这项工作。在名为MyDatabase.mdf的LocalDb中,我创建了另一个名为Amsterdam的表:

[MyDatabase.mdf] [1]

这是我的MainDbContext.cs - 我在这里添加了

public DbSet<Amsterdam> Amsterdam { get; set; }

Home文件夹中,我写了Amsterdam.cshtml,就像这样:

@model IEnumerable<MyFirstWebsite.Models.Amsterdam>
@{
    ViewBag.Title = "Amsterdam";
    var username = User.Identity.Name;
}

<h2>@username's Bestellijst Amsterdam</h2>

@using (Html.BeginForm())
{
    <span>Enter new item: </span>
    <br/>
    <input type="text" name="new_item"/>
    <br/>
    <span>Public post?</span>
    <input type="checkbox" name="check_public"/><br/>
    <br/>
    <input type="submit" value="Add Item"/>
}
<br/>

<table class="table table-bordered table-condensed">
    <thead>
    <tr>
        <th style="text-align: center;">Id Bestelling</th>
        <th style="text-align: center;">Details Bestelling</th>
        <th style="text-align: center;">Time - Ontvangen Bestelling</th>
        <th style="text-align: center;">Time - Verzonden Bestelling</th>
        <th style="text-align: center;">Edit</th>
        <th style="text-align: center;">Delete</th>
        <th style="text-align: center;">Public Post</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td style="text-align: center;"></td>
        <td style="text-align: center;"></td>
        <td style="text-align: center;"></td>
        <td style="text-align: center;"></td>
        <td style="text-align: center;">
            <a href="@Url.Action("Edit", "Home")/">Edit</a>
        </td>
        <td style="text-align: center;">
            <a href="@Url.Action("Delete", "Home")/">Edit</a>
        </td>
        <td style="text-align: center;"></td>
    </tr>
    </tbody>
</table>

Models文件夹中,我创建了Amsterdam.cs,如下所示:

namespace MyFirstWebsite.Models
{
    public class Amsterdam
    {
        [Key]
        public int Id { get; set; }

        public string Details { get; set; }

        public string Date_Posted { get; set; }
        public string Time_Posted { get; set; }
        public string Date_Edited { get; set; }
        public string Time_Edited { get; set; }
        public string Public { get; set; }
        public int User_Id { get; set; }
    }
}

在我的Controllers文件夹中,我的HomeController看起来像这样:

public ActionResult Amsterdam()
{
    return View();
}

[HttpPost]
public ActionResult Amsterdam(Amsterdam list)
{
    string timeToday = DateTime.Now.ToString("h:mm:ss tt");
    string dateToday = DateTime.Now.ToString("M/dd/yyyy");

    if (ModelState.IsValid)
    {
        using (var db = new MainDbContext())
        {
            Claim sessionEmail = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email);
            string userEmail = sessionEmail.Value;
            var userIdQuery = db.Users.Where(u => u.Email == userEmail).Select(u => u.Id);
            var userId = userIdQuery.ToList();

            var dbAmsterdam = db.Amsterdam.Create();
            dbAmsterdam.Details = list.Details;
            dbAmsterdam.Date_Posted = dateToday;
            dbAmsterdam.Time_Posted = timeToday;
            dbAmsterdam.User_Id = userId[0];
            db.Amsterdam.Add(dbAmsterdam);
            db.SaveChanges();
        }
    }
    else
    {
        ModelState.AddModelError("", "Incorrect format has been placed");
    }

    return View();
}

我知道我很接近,因为我的注册确实有效,但我无法让Amsterdam的CRUD工作。当我点击屏幕截图中显示的确定时(当我点击广告项目时):

https://i.stack.imgur.com/XsGws.png

我收到错误

  

用户代码未处理DbUpdateException

https://i.stack.imgur.com/I6kYP.png

1 个答案:

答案 0 :(得分:0)

看来EF正在对您的对象名进行多元化以在数据库中定义表名(来自错误 - Invalid object name dbo.Amsterdams)。

如果您的表名是Amsterdam(单数,没有尾随“s”),则将此数据注释添加到模型类中:

[Table("Amsterdam")]
public class Amsterdam
{         
    [Key]
    public int Id { get; set; }
    .....
}

如果您想完全关闭表名的复数,可以将此行添加到MainDbContext类:

protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
    dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}