大家好,感谢您花时间看我的问题。
我是asp.net MVC和EntityFramework中的新手,因为我从另一个人那里获得了一个项目,而且是在这个技术中。
我的问题是我想添加在客户端对象中存储客户端坐标的可能性。我在我的Client类中添加了Lat和Lng属性,它位于:
//------------------------------------------------------------------------------
// <auto-generated>
// Ce code a été généré à partir d'un modèle.
//
// Des modifications manuelles apportées à ce fichier peuvent conduire à un comportement inattendu de votre application.
// Les modifications manuelles apportées à ce fichier sont remplacées si le code est régénéré.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Uphair.EfModel
{
using System;
using System.Collections.Generic;
public partial class Client
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Client()
{
this.Cards = new HashSet<Card>();
this.NoteClientPartenaires = new HashSet<NoteClientPartenaire>();
this.Notifications = new HashSet<Notification>();
this.Discussions = new HashSet<Discussion>();
this.Reservations = new HashSet<Reservation>();
}
public int IdClient { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Nullable<System.DateTime> DateNaissance { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string PasswordSalt { get; set; }
public string FacebookUid { get; set; }
public string ImageUrl { get; set; }
public Nullable<System.DateTime> DateAjout { get; set; }
public GenreType Genre { get; set; }
public string IdUserMngo { get; set; }
public string Pays { get; set; }
public string Ville { get; set; }
public string Adresse { get; set; }
public string CodePostal { get; set; }
public string TelMobile { get; set; }
public bool Valide { get; set; }
public bool Son { get; set; }
public bool Push { get; set; }
public string IdPhone { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Card> Cards { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<NoteClientPartenaire> NoteClientPartenaires { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Notification> Notifications { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Discussion> Discussions { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Reservation> Reservations { get; set; }
public Nullable<double> Lat { get; set; }
public Nullable<double> Lng { get; set; }
}
}
我还手动将字段Lat和Lng(都是double)添加到我的数据库中的Client表和我的edmx文件中的Client实体。这是来自edmx的我的客户实体:
<EntityType Name="Client">
<Key>
<PropertyRef Name="IdClient" />
</Key>
<Property Name="IdClient" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="FirstName" Type="varchar" MaxLength="255" />
<Property Name="LastName" Type="varchar" MaxLength="255" />
<Property Name="DateNaissance" Type="datetime" Precision="0" />
<Property Name="Email" Type="varchar" MaxLength="255" />
<Property Name="Password" Type="varchar" MaxLength="255" />
<Property Name="PasswordSalt" Type="varchar" MaxLength="255" />
<Property Name="FacebookUid" Type="varchar" MaxLength="255" />
<Property Name="ImageUrl" Type="varchar" MaxLength="255" />
<Property Name="DateAjout" Type="datetime" Precision="0" />
<Property Name="Genre" Type="int" Nullable="false" />
<Property Name="IdUserMngo" Type="varchar" MaxLength="255" />
<Property Name="Pays" Type="varchar" MaxLength="255" />
<Property Name="Ville" Type="varchar" MaxLength="255" />
<Property Name="Adresse" Type="varchar" MaxLength="500" />
<Property Name="CodePostal" Type="varchar" MaxLength="50" />
<Property Name="TelMobile" Type="varchar" MaxLength="50" />
<Property Name="Valide" Type="bool" Nullable="false" />
<Property Name="Son" Type="bool" Nullable="false" />
<Property Name="Push" Type="bool" Nullable="false" />
<Property Name="IdPhone" Type="varchar" MaxLength="255" />
<Property Name="Lat" Type="double" />
<Property Name="Lng" Type="double" />
</EntityType>
当我显示在控制器动作结束时返回的对象时,它已经很好地填充并且其中包含正确的数据。但我的数据库中的字段始终为NULL。
这是来自ClientController的我的动作方法:
public IHttpActionResult PostClient(Inscription model)
{
if (model == null)
{
return BadRequest("Paramètres obligatoire");
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState.Errors()?.FirstOrDefault());
}
//Email existant
if (db.Clients.FirstOrDefault(x => x.Email.Equals(model.Email, StringComparison.InvariantCultureIgnoreCase))
!= null)
{
return BadRequest(UphairResources.EmailAlreadyExist);
}
string url = @"https://maps.googleapis.com/maps/api/geocode/json";
RestClient clientRest = new RestClient(url);
RestRequest request = new RestRequest();
request.AddParameter("address",
"" + model.Ville + "+" + model.Adresse + "+" + model.CodePostal);
var result = clientRest.Execute<ResultAdress>(request);
if (result.Data.status != "OK")
{
return BadRequest("Aucune correspondance n'a été trouvée pour votre adresse postale, vérifiez-là.");
}
var client = new Client
{
Email = model.Email,
FirstName = model.Prenom,
LastName = model.Nom,
Genre = model.Genre,
DateAjout = DateTime.Now,
Valide = true,
ImageUrl = "https://res.cloudinary.com/**********/image/upload/v1509641859/profile_oqcfnd_zey6xm.png",
TelMobile = model.TelMobile,
Adresse = model.Adresse,
Ville = model.Ville,
CodePostal = model.CodePostal,
Pays = "France"
};
client.Lat = result.Data.results.FirstOrDefault()?.geometry.location.lat;
client.Lng = result.Data.results.FirstOrDefault()?.geometry.location.lng;
var crypto = new SimpleCrypto.PBKDF2 { HashIterations = 10000 };
crypto.Compute(model.Password);
client.Password = crypto.HashedText;
client.PasswordSalt = crypto.Salt;
//Add client a Save
db.Clients.Add(client);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = client.IdClient }, client); // Here client has the correct data in Client.Lat and Client.Lng
}
提前感谢您的支持:)