C# Heritage:EntityFramework中使用DBContext的CRUD操作
我正在尝试使用DbContext在实体框架中实现CRUD操作 我有一个名为“offreLocation”的类继承自一个名为“publication”的super类 我没有收到任何错误,但在单击“创建”按钮后,我仍然拥有“创建”视图,尽管我在创建后指定了列表视图的重定向 这里是我的创建函数C# Heritage:EntityFramework中使用DBContext的CRUD操作,c#,asp.net,entity-framework,asp.net-mvc-4,C#,Asp.net,Entity Framework,Asp.net Mvc 4,我正在尝试使用DbContext在实体框架中实现CRUD操作 我有一个名为“offreLocation”的类继承自一个名为“publication”的super类 我没有收到任何错误,但在单击“创建”按钮后,我仍然拥有“创建”视图,尽管我在创建后指定了列表视图的重定向 这里是我的创建函数 // POST: /OffreLocation/Create [HttpPost] public ActionResult CreateOffreLocation(OffreLocation
// POST: /OffreLocation/Create
[HttpPost]
public ActionResult CreateOffreLocation(OffreLocation offreLocation)
{
try
{
db.PublicationSet.Add(offreLocation);
db.SaveChanges();
return RedirectToAction("ListOffreLocation");
}
catch
{
return View();
}
}
我的创建视图:
@model COM.MENNILIK.Models.OffreLocation
@{
ViewBag.Title = "CreateOffreLocation";
}
<h2 style="text-align:center">Nouvelle offre de location </h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Publication_ID, new { @class = "control-label col-md-2" })
<div class="col-lg-1">
@Html.EditorFor(model => model.Publication_ID)
@Html.ValidationMessageFor(model => model.Publication_ID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Statut, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Statut)
@Html.ValidationMessageFor(model => model.Publication_Statut)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Meublee, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Meublee)
@Html.ValidationMessageFor(model => model.Publication_Meublee)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Descriptif, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Descriptif)
@Html.ValidationMessageFor(model => model.Publication_Descriptif)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_ContactParAgence, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_ContactParAgence)
@Html.ValidationMessageFor(model => model.Publication_ContactParAgence)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Maps_Latitude, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Maps_Latitude)
@Html.ValidationMessageFor(model => model.Publication_Maps_Latitude)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Maps_Longitude, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Maps_Longitude)
@Html.ValidationMessageFor(model => model.Publication_Maps_Longitude)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_TypeLog, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_TypeLog)
@Html.ValidationMessageFor(model => model.OffreLocation_TypeLog)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_Sante, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_Sante)
@Html.ValidationMessageFor(model => model.OffreLocation_Sante)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_Loyer, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_Loyer)
@Html.ValidationMessageFor(model => model.OffreLocation_Loyer)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_DateDisponibilite, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_DateDisponibilite)
@Html.ValidationMessageFor(model => model.OffreLocation_DateDisponibilite)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_Superficie, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_Superficie)
@Html.ValidationMessageFor(model => model.OffreLocation_Superficie)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_NbreChambre, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_NbreChambre)
@Html.ValidationMessageFor(model => model.OffreLocation_NbreChambre)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_NbrePieces, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_NbrePieces)
@Html.ValidationMessageFor(model => model.OffreLocation_NbrePieces)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "ListOffreLocation")
</div>
@model COM.MENNILIK.Models.off
@{
ViewBag.Title=“CreateOffreLocation”;
}
新安置主任
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@LabelFor(model=>model.Publication_ID,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Publication\u ID)
@Html.ValidationMessageFor(model=>model.Publication\u ID)
@LabelFor(model=>model.Publication_Statut,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Publication\u Statut)
@Html.ValidationMessageFor(model=>model.Publication\u Statut)
@LabelFor(model=>model.Publication_Meublee,new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Publication\u Meublee)
@Html.ValidationMessageFor(model=>model.Publication\u Meublee)
@LabelFor(model=>model.Publication_Descriptif,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Publication\u Descriptif)
@Html.ValidationMessageFor(model=>model.Publication\u Descriptif)
@Html.LabelFor(model=>model.Publication_ContactParAgence,new{@class=“control label col-md-2”})
@EditorFor(model=>model.Publication\u contactparance)
@Html.ValidationMessageFor(model=>model.Publication\u contactparance)
@LabelFor(model=>model.Publication_Maps_Latitude,new{@class=“control label col-md-2”})
@EditorFor(model=>model.Publication\u Maps\u Latitude)
@Html.ValidationMessageFor(model=>model.Publication\u Maps\u Latitude)
@LabelFor(model=>model.Publication_Maps_Longitude,new{@class=“control label col-md-2”})
@EditorFor(model=>model.Publication\u Maps\u经度)
@Html.ValidationMessageFor(model=>model.Publication\u Maps\u经度)
@LabelFor(model=>model.off\u TypeLog,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.off\u TypeLog)
@Html.ValidationMessageFor(model=>model.off\u TypeLog)
@LabelFor(model=>model.off,新的{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.off)
@Html.ValidationMessageFor(model=>model.off\u Sante)
@LabelFor(model=>model.offloyer,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.offer\u Loyer)
@Html.ValidationMessageFor(model=>model.offloyer)
@LabelFor(model=>model.off\u DateDisponibilite,新的{@class=“control label col-md-2”})
@Html.EditorFor(model=>model.off\u DateDisponibilite)
@Html.ValidationMessageFor(model=>model.OffreLocation\u DateDisponibilite)
@LabelFor(model=>model.off\u Superficie,新的{@class=“control label col-md-2”})
@EditorFor(model=>model.off\u Superficie)
@Html.ValidationMessageFor(model=>model.off\u Superficie)
@LabelFor(model=>model.off\u NbreChambre,新的{@class=“control label col-md-2”})
@Html.EditorFor(model=>model.off\u NbreChambre)
@Html.ValidationMessageFor(model=>model.OffreLocation\u NbreChambre)
@LabelFor(model=>model.offreplaces,新的{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.off\u nbrelieces)
@Html.ValidationMessageFor(model=>model.OffreLocation\u nbreleces)
}
@ActionLink(“返回列表”、“列表”)
为了获得更高的精度,这里是我的模型容器
public partial class Model1Container : DbContext
{
public Model1Container()
: base("name=Model1Container")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Utilisateur> UtilisateurSet { get; set; }
public DbSet<Abonnement> AbonnementSet { get; set; }
public DbSet<AbonnementHistorique> AbonnementHistoriqueSet { get; set; }
public DbSet<ColocataireIdeal> ColocataireIdealSet { get; set; }
public DbSet<Publication> PublicationSet { get; set; }
public DbSet<Quartier> QuartierSet { get; set; }
public DbSet<Ville> VilleSet { get; set; }
public DbSet<RegionProvince> RegionProvinceSet { get; set; }
public DbSet<Photo> PhotoSet { get; set; }
public DbSet<MessageLocation> MessageLocationSet { get; set; }
public DbSet<MessageColocation> MessageColocationSet { get; set; }
}
public分部类model1容器:DbContext
{
公共模型1容器()
:base(“name=Model1Container”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共数据库集使用过程集{get;set;}
公共DbSet AbonnementSet{get;set;}
public DbSet abonnementhistoryqueset{get;set;}
公共DbSet colocatairedalset{get;set;}
public DbSet PublicationSet{get;set;}
公共DbSet QuartierSet{get;set;}
公共DbSet-VilleSet{get;set;}
公共数据库集RegionProvinceSet{get;set;}
公共数据库集PhotoSet{get;set;}
公共DbSet MessageLocationSet{get;set;}
public DbSet messagecolocatiost{get;set;}
}
“出版”模式
公共部分类发布
{
公开出版物()
{
this.Photo=newhashset();
}
公共int发布_ID{get;set;}
公共字符串发布_Statut{get;set;}
公共布尔出版物_Meublee{get;set;}
公共字符串发布_Descriptif{get;set;}
公共布尔出版物{get;set;}
公共双出版物地图
public partial class Publication
{
public Publication()
{
this.Photo = new HashSet<Photo>();
}
public int Publication_ID { get; set; }
public string Publication_Statut { get; set; }
public bool Publication_Meublee { get; set; }
public string Publication_Descriptif { get; set; }
public bool Publication_ContactParAgence { get; set; }
public double Publication_Maps_Latitude { get; set; }
public double Publication_Maps_Longitude { get; set; }
public virtual Quartier Quartier { get; set; }
public virtual ICollection<Photo> Photo { get; set; }
}
public partial class OffreLocation : Publication
{
public OffreLocation()
{
this.Locataire = new HashSet<Locataire>();
this.DemandeLocation = new HashSet<DemandeLocation>();
this.DemandeLocation1 = new HashSet<DemandeLocation>();
this.DemandeLocation2 = new HashSet<DemandeLocation>();
this.DemandeLocation3 = new HashSet<DemandeLocation>();
}
public string OffreLocation_TypeLog { get; set; }
public string OffreLocation_Sante { get; set; }
public double OffreLocation_Loyer { get; set; }
public System.DateTime OffreLocation_DateDisponibilite { get; set; }
public double OffreLocation_Superficie { get; set; }
public short OffreLocation_NbreChambre { get; set; }
public short OffreLocation_NbrePieces { get; set; }
public virtual ICollection<Locataire> Locataire { get; set; }
public virtual Proprietaire Proprietaire { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation1 { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation2 { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation3 { get; set; }
}
db.PublicationSet.Add(offreLocation);
db.SaveChanges();
catch
{
return View();
}