【问题标题】:EF can't save into data from list<address> to databaseEF 无法将数据从 list<address> 保存到数据库
【发布时间】:2014-02-12 04:26:12
【问题描述】:

我从 formcollectionn 获取数据并将其存储到列表中我现在的问题是我的上下文。saveshange 没有将数据保存到地址数据库中是我的上下文在保存后为 0 吗?我真的不知道我的问题是因为先使用 sql构建我的实体框架?

  public class AddressController : Controller
    {

        private readonly CustomersDBEntities context = new CustomersDBEntities();
        //
        // GET: /Address/

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

        // 
        // GET: /Address/Welcome/ 

        public string Welcome()
        {
            return "This is the Welcome action method...";
        }

        [HttpPost]
        public ActionResult Create(Address address)
        {
            //Loop through the request.forms

            var Addesslist = new List<Address>();
            for (int i = 1; i <= Request.Form.Count; i++)
            {
                var street = Request.Form["street_0" + i + ""];
                var city = Request.Form["city_0" + i + ""];
                var postalCode = Request.Form["postalCode_0" + i + ""];
                var province = Request.Form["province_0" + i + ""];
                var personID = 1;
                if (street != null && city != null && postalCode != null && province != null)
                {
                    Addesslist.Add(new Address { Street = street, City = city, Province = province, PostalCode = postalCode, PersonID = personID });

                    Addesslist.ForEach(p => context.SaveChanges());
                    if (context.SaveChanges() == Addesslist.Count)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        //Redirect to an error page or

                    }
                }
                else
                {
                    break;
                }

            }
            return RedirectToAction("Index");
        }
    }
} 

【问题讨论】:

  • 您正在为Addesslist 中的每个项目保存一次。当您到达if 语句中的context.SaveChanges() 时,已经没有更多需要保存的更改了。
  • 只是想问一下有没有批量插入线?
  • @rikitikitik 已删除 if 但我的数据库中仍然没有数据
  • 你调试过你的代码吗?您是否 100% 确定正在创建 Address 对象?我注意到的另一件事是,如果在任何时候 (street != null &amp;&amp; city != null &amp;&amp; postalCode != null &amp;&amp; province != null) 为假,你就会打破循环。

标签: c# asp.net-mvc entity-framework asp.net-mvc-4


【解决方案1】:

您可以使用此代码 sn-p 使其工作

    [HttpPost]
    public ActionResult Create(Address address)
    {
        //Loop through the request.forms

        var Addesslist = new List<Address>();
        for (int i = 1; i <= Request.Form.Count; i++)
        {
            var street = Request.Form["street_0" + i + ""];
            var city = Request.Form["city_0" + i + ""];
            var postalCode = Request.Form["postalCode_0" + i + ""];
            var province = Request.Form["province_0" + i + ""];
            var personID = 1;
            if (street != null && city != null && postalCode != null && province != null)
            {

                try
                {
                    context.Addresses.Add(new Address
                    {
                        Street = street,
                        City = city,
                        Province = province,
                        PostalCode = postalCode,
                        PersonID = personID
                    });
                    context.SaveChanges();
                }
                catch (Exception exc)
                {

                }
            }
            else
            {
                break;
            }

        }
        return RedirectToAction("Index");
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多