【发布时间】:2016-05-04 09:08:34
【问题描述】:
我的模型
public class Hotel
{
public int Id { get; set; }
[Required]
[Display(Name="Hotel Name")]
public string HotelName {get;set;}
[Required]
public string Address { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:N6}", ApplyFormatInEditMode = true)]
[RegularExpression(@"\d{1,10}(\.\d{1,6})", ErrorMessage = "Invalid Latitude")]
public Decimal Latitude { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:N6}", ApplyFormatInEditMode = true)]
[RegularExpression(@"\d{1,10}(\.\d{1,6})", ErrorMessage = "Invalid Longitude")]
public Decimal Longitude { get; set; }
[Required]
[RegularExpression(@"\d{10,20}", ErrorMessage = "Invalid Number")]
public string Telephone { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
}
问题在于纬度和经度。它们在 SQL Server DB 中的格式是十进制 (11, 6)。所以当我在创建中给出纬度 = 41.32056 和经度 = 19.805542 的值时,我调试并看到模型构建正确
[HttpPost]
public ActionResult Create(Hotel hotel)
{
try
{
// TODO: Add insert logic here
if (ModelState.IsValid)
{
db.Hotels.Add(hotel);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
但存储在 DB 中的值是纬度 = 41.320000 和经度 = 19.800000。它应该是纬度 = 41.32056 和经度 = 19.805542。 我错过了什么。
我的 DbContext 类看起来像这样
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Hotel> Hotels { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Booking> Bookings { get; set; }
public DbSet<Audit> Audit { get; set; }
}
我从未使用过 DbModelBuilder。 我必须更改我的 DbContext 类吗?
【问题讨论】:
-
你能发布你的模型构建器的代码吗?数据库中的列类型是什么?
标签: c# asp.net-mvc entity-framework linq