【问题标题】:System.Data.Entity.Spatial.DbGeography - No Parameterless ConstructorSystem.Data.Entity.Spatial.DbGeography - 无无参数构造函数
【发布时间】:2017-10-12 20:26:29
【问题描述】:

我有一个提交页面,其中一个表单字段是纬度/经度坐标。我将它们存储在名为“Location”的 System.Data.Entity.Spatial.DbGeography 中,并呈现如下字段:

<div class="form-group">
            @Html.LabelFor(model => model.Location.Longitude, new { @class = "control-label col-md-2" })
            <div class="col-sm-3">
                @Html.TextBoxFor(model => model.Location.Longitude)
                <img class="load-icon" src="~/Content/Loading.gif" />
                @Html.ValidationMessageFor(model => model.Location.Longitude)
            </div>

            @Html.LabelFor(model => model.Location.Latitude, new { @class = "control-label col-md-2" })
            <div class="col-sm-3">
                @Html.TextBoxFor(model => model.Location.Latitude)
                <img class="load-icon" src="~/Content/Loading.gif" />
                @Html.ValidationMessageFor(model => model.Location.Latitude)
            </div>
        </div>

在我的控制器中,我收到另一边的表单:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "...Location...")] Report report)

但每次我使用表单中的值进行 POST 时,它都会引发异常,指出某个对象(它无法告诉我哪个)没有无参数构造函数。当我从绑定字符串中删除“位置”但保留所有其他属性(在我的示例中省略)时,它工作正常。

这似乎表明问题在于它正在尝试创建一个 DbGeography 以放入“报告”,但由于无法实例化它而失败。它是否正确?如果是这样,我怎样才能避免这个错误?我宁愿不在我的数据库中使用自定义类,但我想不出任何其他选择。

【问题讨论】:

  • DBGeography 有默认构造函数吗?
  • 它不是没有参数的公共构造函数
  • 所以,如果这不是公共构造函数,你的期望是什么 - 这就是你的答案

标签: c# asp.net-mvc-5 sqlgeography


【解决方案1】:

使用 sqlQueryCommand 的最佳方式 这代码 100% 有效:

    var id = form["id"];
            var name = form["name"];
            var lat = form["Location.Latitude"];
            var lng = form["Location.Longitude"];
            db.Database.ExecuteSqlCommand("insert into [dbo].[schoolinfo] values ('" + id + "','" + name + "',geography::Point(" + lat + ", " + lng + ", 4326))");

【讨论】:

  • 不,这根本不安全,不要这样做。
  • 为什么不呢? @JOSEFtw
  • 我在绑定 dbGeography 时遇到问题,所以我选择 formcollection 和 sqCommand
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 2013-12-24
  • 1970-01-01
相关资源
最近更新 更多