Entity Framework Id Value assign: - asp.net-mvc-3

I have an entity defined as in a db First Model:
public class Merlin_BR_Condiciones_Item
{
public int IntIdGrupoCondiciones { get; set; }
public string StrCondicion { get; set; }
[Key]
public int IntIdCondicion { get; set; }
public virtual Merlin_BR_Condiciones_Item_Grupos Merlin_BR_Condiciones_Item_Grupos { get; set; }
}
And A controller generated automatically that has this create action:
public ActionResult Create(int pIntIdGrupoCondiciones = 0 )
{
ViewBag.IntIdGrupoCondiciones = new SelectList(db.Merlin_BR_Condiciones_Item_Grupos, "IntIdGrupoCondiciones", "StrDescripcionGrupo");
return View();
}
[HttpPost]
public ActionResult Create(Merlin_BR_Condiciones_Item merlin_br_condiciones_item)
{
if (ModelState.IsValid)
{
//================================================================================
// This section add the current key to IntIdCondicion
//================================================================================
var max = from c in db.Merlin_BR_Condiciones_Item
select c;
merlin_br_condiciones_item.IntIdCondicion = max.AsQueryable().Max(x => x.IntIdCondicion) + 1;
//================================================================================
db.Merlin_BR_Condiciones_Item.Add(merlin_br_condiciones_item);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.IntIdGrupoCondiciones = new SelectList(db.Merlin_BR_Condiciones_Item_Grupos, "IntIdGrupoCondiciones", "StrDescripcionGrupo", merlin_br_condiciones_item.IntIdGrupoCondiciones);
return View(merlin_br_condiciones_item);
}
This Entity has an Id Column assigned manually in the HttPost (create action).
The problem is that an error was idicating me that Can't insert NULL Value in IntIdCondicion column.
Following step by step the code the value allways return a valid key.
Tks for your help.

By default EF expects that all integer primary keys are generated in a database. So modify your mapping and tell EF that your primary key is not autogenerated:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int IntIdCondicion { get; set; }
If you are using EDMX you must configure StoreGeneratedPattern to None in IntIdCondicion properties.

Related

EF Core: loading related entities - circular dependency

I have 2 related entities in EF Core (database first design from an existing database) and having trouble loading one-many relationship - it's a webapi ASP.NET core 1.0 application
Brand Entity
[Table("tblBranding")]
public class Brand {
[Key]
[Column("brandingId")]
public int BrandId { get; set; }
[Column("BrandingActive")]
public bool Active { get; set; }
[JsonIgnore]
[Column("DeadBrand")]
public bool DeadBrand { get; set; }
[Column("BrandingSiteTitle")]
public string Name { get; set; }
//navigation properties
public virtual ICollection<Event> Events { get; set; }
}
Event entity:
[Table("tblEvents")]
public class Event
{
public int EventId { get; set; }
[Column("eventActive")]
public bool Active { get; set; }
[Column("eventName")]
public string Name { get; set; }
public DateTime EventCloseDate {get;set;}
public int PaxAllocationLimit { get; set; }
//navigation properties
[Column("BrandingId")]
public int BrandId { get; set; }
public virtual Brand Brand { get; set; }
public virtual ICollection<Session> Sessions { get; set; }
}
code from for FLUID API in OnModelCreating in DbContext:
modelBuilder.Entity<Event>()
.HasOne(e => e.Brand)
.WithMany(b => b.Events).HasForeignKey(e=>e.BrandId);
public virtual DbSet<Brand> Brands { get; set; }
public virtual DbSet<Event> Events { get; set; }
code from BrandsController:
[HttpGet]
public IActionResult Get() {
//var brands = from b in _context.Brands
// where b.Active == true
// orderby b.BrandName
// select b;
var brands = _context.Brands.Include(e => e.Events).Where(b => b.Active == true).OrderBy(b => b.Name);
return new ObjectResult(brands);
}
code from EventsController
// GET: api/values
[HttpGet("{id:int?}")]
public IActionResult Get(int? id) {
var events = from e in _context.Events
where e.Active == true
orderby e.Name
select e;
if (!events.Any()) {
return HttpNotFound();
}
if (id != null) {
events = events.Where(e => e.EventId == id).OrderBy(e => 0);
if (events.Count() == 0) { return HttpNotFound(); }
return new ObjectResult(events);
}
else {
return new ObjectResult(events);
}
}
When I try to load brands through the API, I get an exception:
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (80ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [t].[EventId], [t].[EventCloseDate], [t].[eventActive], [t].[BrandingId], [t].[EventId1], [t].[eventName], [t].[PaxAllocationLimit]
FROM [tblEvents] AS [t]
INNER JOIN (
SELECT DISTINCT [e].[BrandingSiteTitle], [e].[brandingId]
FROM [tblBranding] AS [e]
WHERE [e].[BrandingActive] = 1
) AS [e] ON [t].[BrandingId] = [e].[brandingId]
ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]
Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory: Error: An exception occurred in the database while iterating the results of a query.
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'EventId1'.
Apart from this, is there a way to load related entities without using the "Includes" ? If I do not use include and use the standard LINQ query, the related entities are loaded as NULL
UPDATE
I'm now getting an invalid column error - i noticed in my previous code I hadn't used virtual on the ICollection in brand
now i can't figure out why is it generating EventId1 column in the SQL
EF 7 version is 1.0.0-rc1-final
UPDATE-2
After playing around with the code the Exception changed to circular dependency exception in code given the exact same code as above - I don't know why it was generating the invalid column name earlier (EventId1)
Answering my own question here - figured it out-
the 2 entities used here, I've used fully defined relationships in the EF 7
- however the JSON serializer doesn not like that,as this createsa circular dependancy - Brand Contains Events List, and each event also contains the parent brand property -
so the solution here was to add [JsonIgnore] attribute to relationship properties on the child
updated Events class:
[Table("tblEvents")]
public class Event
{
public int EventId { get; set; }
[Column("eventActive")]
public bool Active { get; set; }
[Column("eventName")]
public string Name { get; set; }
public DateTime EventCloseDate {get;set;}
public int PaxAllocationLimit { get; set; }
//navigation properties
[JsonIgnore]
[Column("brandingId")]
public virtual int BrandId { get; set; }
[JsonIgnore]
public virtual Brand Brand { get; set; }
//public virtual ICollection<Session> Sessions { get; set; }
}

Foreign Class not populating after save in mvc 4

I am saving data using Ajax and at the same time I want it to display in list. below is Class of City
public class City
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int StateId { get; set; }
[ForeignKey("StateId")]
public virtual State state { get; set; }
}
my Post method is
[HttpPost]
public ActionResult EditCity(City obj)
{
try
{
aRepository.Save(obj);
var db = aRepository.Citys.FirstOrDefault(c => c.Id == obj.Id);
return PartialView("_iCity", db);
}
catch (Exception e)
{
return Json("", JsonRequestBehavior.AllowGet);
}
}
Here db.state = null, it's not populating.
Should get you the state detail by :
or
var db = aRepository.Citys.FirstOrDefault(c => c.Id == obj.Id).Include(x=>x.StateId);
When you post the data for your server, your ViewModel should has StateId, if you are passing the Object state into it, by default Etity Framework will not persist.
I suppose you are using Lazy Loading , so you dont need to user Include to get the State entity

Entity framework is making the id null on insert

I'm getting the following error when I try to insert a new row in one of my relational tables. I have the following two models:
public class CompanyCredit
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int creditId { get; set; }
public int planCredit { get; set; }
public DateTime? PlanCreditExpirationDate { get; set; }
}
And
public class CompanyInformation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[Required]
[DisplayName("Company Name:")]
public string companyName { get; set; }
public string timeZone { get; set; }
//navigation Properties
public virtual CompanyCredit Credits { get; set; }
}
And this Relation in the dbContext
modelBuilder.Entity<CompanyInformation>().HasOptional(e => e.Credits);
I'm trying to add a record inside CompanyCredit table like so:
if (_company.Credits == null)
{
var _credits = new CompanyCredit();
_credits.planCredit = 200;
_credits.PlanCreditExpirationDate = System.DateTime.UtcNow.AddMonths(1);
_company.Credits = _credits;
repo.InsertOrUpdate(_company, User.Identity.Name);
}
And Finally Insert or update just marks Company as changed and _credit as added like so:
_db.Entry(_credits).State = System.Data.EntityState.Added;
_db.Entry(Company).State = System.Data.EntityState.Modified;
_db.SaveChanges();
When this runs I get the following Error that I just can't seem to find the reason to.
Cannot insert the value NULL into column 'creditId', table 'Project.dbo.CompanyCredits'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Thank in advanced for your help.
I found the problem was in the attribute [DatabaseGenerated(DatabaseGeneratedOption.Identity)] this should have been [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
I thought I would post this so others might benefit from it.
Could you please try reversing the order of entity state modification, just before the saveChanges call
_db.Entry(Company).State = System.Data.EntityState.Modified;
_db.Entry(_credits).State = System.Data.EntityState.Added;
_db.SaveChanges();

Entity FrameWork 4.1 Exception

This is my code:
public void DeleteFolder(Entities.DocumentFolder folder)
{
DeleteFilesFromServer(folder.Id);
_dbContext.Entry(folder).State = EntityState.Deleted;
_dbContext.SaveChanges();
}
public void DeleteFilesFromServer(int id)
{
var allDocuments = _dbContext.Document.Where(x => x.FolderId == id).ToList();
foreach (var filePath in allDocuments.Select(document => HttpContext.Current.Server.MapPath("~/Documents/") + document.DocumentFileName).Where(System.IO.File.Exists))
{
System.IO.File.Delete(filePath);
}
}
public class DocumentFolder
{
public DocumentFolder()
{
Documents=new List<Document>();
}
public int Id { get; set; }
public string FolderName { get; set; }
public int ParentFolderId { get; set; }
public List<Document> Documents { get; set; }
}
public class Document
{
public int Id { get; set; }
public string DocumentName { get; set; }
public string DocumentFileName { get; set; }
public int FolderId { get; set; }
public virtual DocumentFolder Folder { get; set; }
}
By executing the delete operation I got the following exception:
System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
If I remove the DeleteFilesFromServer(int id) method the deletion working. Can someone help me?
If you want to delete the DocumentFolder, you need to delete the Document objects related to the DocumentFolder because in your model the field Folder is not nullable. This happens only if the dbContext knows that the the Document objects exist, i.e. if you load the documents with the Select method.

Update composite foreign key

I have a self referencing model with composite primary key in ASP.NET MVC 3 using code first approach:
public class Area
{
[Key, Column(Order=0)]
public int Id1 { get; set; }
[Key, Column(Order=1)]
public int Id2 { get; set; }
public string Name { get; set; }
public virtual Area Parent { get; set; }
}
And I would like to have a controller with create and edit operations that can work with all properties, including the composite Parent (that must be previously added to the database).
I managed to get the create method running, but for editing the complex field doesn't want to update. The input data for the following method successfully parses to the object area that also has area.Parent.Id1 and area.Parent.Id2 set.
Current code that doesn't save changes for modifications to Parent:
[HttpPost]
public ActionResult Edit(Area area)
{
try
{
if (ModelState.IsValid)
{
if (area.Parent != null)
{
area.Parent = db.Areas.Find(area.Parent.Id1, area.Parent.Id2);
if (area.Parent == null)
throw new NotFoundException();
// need to mark it as modified...
}
db.Entry(area).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (NotFoundException)
{
//...
}
return View(area);
}
I found a kind of workaround if anyone needs it. Add this to the model:
[ForeignKey("Parent"), Column(Order = 0)]
public int Parent_Id1 { get; set; }
[ForeignKey("Parent"), Column(Order = 1)]
public int Parent_Id2 { get; set; }
And add this where it says "need to mark it as modified":
area.Parent_Id1 = area.Parent.Id1;
area.Parent_Id2 = area.Parent.Id2;

Resources