In our SQL database we have a table with a composite key that includes fields we need to update from time to time. It's my understanding that, since we are using the Entity Framework, I need to remove the record from the database first then add the row back to the table.
Below is the simple method I created to handle an "update" since there are numerous methods that will be carrying this out. However, once the .SaveChanges() method is called after the .Remove I get the DbUpdateConcurrencyException:
Store update, insert, or delete statement affected an unexpected number of
rows (0). Entities may have been modified or deleted since entities were loaded.
Refresh ObjectStateManager entries.
Not sure what I'm doing wrong as I'm trying to remove the record, then perform the updates, then add the record back.
Here's the method that calls the remove/edit methods. When this method is called, the record hasn't been altered in any way shape or form yet.
private static void ProcessAllChanges(ZipCodeIndex information, ZipCodeTerritory zipToUpdate)
{
try
{
RemoveRecord(zipToUpdate);
if (!string.IsNullOrWhiteSpace(information.newTerritory)) zipToUpdate.IndDistrnId = information.newTerritory;
if (!string.IsNullOrWhiteSpace(information.newStateCode)) zipToUpdate.StateCode = information.newStateCode;
if (!string.IsNullOrWhiteSpace(information.newDescription)) zipToUpdate.DrmTerrDesc = information.newDescription;
if (!string.IsNullOrWhiteSpace(information.newChannelCode)) zipToUpdate.ChannelCode = information.newChannelCode;
if (zipToUpdate.EndDate == DateTime.MinValue) zipToUpdate.EndDate = DateTime.MaxValue;
EditRecord(zipToUpdate);
_updated++;
}
catch (DbEntityValidationException dbEx)
{
_msg += "Error during update; ";
EventLog.WriteEntry("Monet", "Error during ProcessAllChanges: " + zipToUpdate.ToString() + " |EX| " + dbEx.Message);
}
catch (Exception ex)
{
_msg += "Error during update; ";
EventLog.WriteEntry("Monet", "Error during ProcessAllChanges: " + zipToUpdate.ToString() + " |MESSAGE| " + ex.Message);
}
}
And here are the two helper methods that get called
public static void RemoveRecord(ZipCodeTerritory zipCode)
{
_db = new AgentResources();
_db.ZipCodeTerritory.Attach(zipCode);
_db.ZipCodeTerritory.Remove(zipCode);
_db.SaveChanges();
}
public static void EditRecord(ZipCodeTerritory zipCode)
{
_db = new AgentResources();
_db.ZipCodeTerritory.Add(zipCode);
_db.SaveChanges();
}
EDIT
Based on a couple comments below I attempted to create a separate instance of the context object, however i received the same error using this method:
public static void RemoveRecord(ZipCodeTerritory zipCode)
{
using (AgentResources deleteMe = new AgentResources())
{
deleteMe.ZipCodeTerritory.Attach(zipCode);
deleteMe.ZipCodeTerritory.Remove(zipCode);
deleteMe.SaveChanges();
}
}
Second Edit
Here is the upper most method which calls the ProcessAllChanges method I posted above.
public static string TerritoryOnly(ZipCodeIndex updateZip)
{
if (!string.IsNullOrWhiteSpace(updateZip.newEffectiveDate) || !string.IsNullOrWhiteSpace(updateZip.newEndDate))
{
return "Neither effective or end date can be present if updating Territory Code only; ";
}
RefreshProperties();
foreach (var zipCode in updateZip.displayForPaging.Where(x => x.Update))
{
ProcessAllChanges(updateZip, zipCode);
}
_msg += _updated + " record(s) updated; ";
return _msg;
}
Third Edit
Per request here is the full class definition of AgentResources, our DbContext object
namespace Monet.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class AgentResources : DbContext
{
public AgentResources()
: base("name=AgentResources")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<AgentContEd> AgentContEd { get; set; }
public DbSet<ContEdCourse> ContEdCourse { get; set; }
public DbSet<Course> Course { get; set; }
public DbSet<CourseToProduct> CourseToProduct { get; set; }
public DbSet<Product> Product { get; set; }
public DbSet<ProcessControl> ProcessControl { get; set; }
public DbSet<AgentIdToTradingPartner> AgentIdToTradingPartner { get; set; }
public DbSet<TradingPartner> TradingPartner { get; set; }
public DbSet<Notes> Notes { get; set; }
public DbSet<CourseMaterials> CourseMaterials { get; set; }
public DbSet<TransactionLog> TransactionLog { get; set; }
public DbSet<Agent> Agent { get; set; }
public DbSet<AgentIdentification> AgentIdentification { get; set; }
public DbSet<BatchDashboard> BatchDashboard { get; set; }
public DbSet<BatchPrograms> BatchPrograms { get; set; }
public DbSet<FollowUpItems> FollowUpItems { get; set; }
public DbSet<sysdiagrams> sysdiagrams { get; set; }
public DbSet<AgentProductTraining> AgentProductTraining { get; set; }
public DbSet<Channel> Channel { get; set; }
public DbSet<RelationshipCodes> RelationshipCodes { get; set; }
public DbSet<DropDownValues> DropDownValues { get; set; }
public DbSet<QueueUpdates> QueueUpdates { get; set; }
public DbSet<MarketingLookup> MarketingLookup { get; set; }
public DbSet<TransmissionHistory> TransmissionHistory { get; set; }
public DbSet<AgentTransmission> AgentTransmission { get; set; }
public DbSet<ZipCodeTerritory> ZipCodeTerritory { get; set; }
}
}
Here is the ZipCodeTerritory class
public partial class ZipCodeTerritory
{
public string ChannelCode { get; set; } //Composite key field
public string DrmTerrDesc { get; set; }
public string IndDistrnId { get; set; }
public string StateCode { get; set; } //Composite key field
public string ZipCode { get; set; } //Composite key field
public System.DateTime? DisplayEndDate { get; set; }
public System.DateTime EndDate { get; set; } //Composite key field
public System.DateTime EffectiveDate { get; set; }
public string LastUpdateId { get; set; }
public Nullable<System.DateTime> LastUpdateDate { get; set; }
}
Try this :
public static void RemoveRecord(ZipCodeTerritory zipCode)
{
using(var _newdb = new AgentResources())
{
ZipCodeTerritory zipCodeRemove = new ZipCodeTerritory();
zipCodeRemove.channelCode = zipCode.channelCode;
zipCodeRemove.stateCode = zipCode.stateCode;
zipCodeRemove.zipCode= zipCode.zipCode;
zipCodeRemove.endDate = zipCode.endDate;
_newdb.ZipCodeTerritory.Attach(zipCodeRemove);
_newdb.ZipCodeTerritory.Remove(zipCodeRemove);
//((IObjectContextAdapter)_newdb).ObjectContext.Refresh(
// RefreshMode.ClientWins
//, zipCode);
_newdb.SaveChanges();
}
}
Related
At first I'm sorry for using my native language in Code, but it's my University project and our project Leader ordered us to write like this.
I'm working on Database project using Entity Framework and C#.
In short, I've created class named "Osoba" and "Klient" class which inherits from "Osoba".
Problem is that when I'm trying to add new "Klient" to database I'm still getting error as following:
System.Data.Entity.Infrastructure.DbUpdateException: „An error occurred while updating the entries. See the inner exception for details.”
SqlException: Cannot insert explicit value for identity column in table 'Klient' when IDENTITY_INSERT is set to OFF.
I've researched similar problems in web, but all of them were appearing because of "hard-coding" ID while adding new object to table.. and I'm actually not doing this.
Here is Osoba class:
[Table("Osoba")]
public class Osoba
{
public int ID { get; set; }
public string Imie { get; set; }
public string Nazwisko { get; set; }
public string Telefon { get; set; }
public string Adres { get; set; }
public string Mail { get; set; }
public int IloscTransakcji { get; set; }
public string Typ { get; set; }
public override string ToString()
{
return "Imie: " + Imie + "\t Nazwisko: " + Nazwisko + "\t Adres: " + Adres;
}
}
Klient class:
[Table("Klient")]
public class Klient: Osoba
{
public int ID { get; set; }
public string Pracownik { get; set; }
public int Sprzedane { get; set; }
public int Kupione { get; set; }
public string Preferencje { get; set; }
public override string ToString()
{
return "Obslugujacy pracownik: " + Pracownik + "\t Sprzedane: " + Sprzedane.ToString() + "\t Kupione: " + Kupione.ToString();
}
}
My Database Context:
public class BazyDanychContext : DbContext
{
public BazyDanychContext() : base("ProjektBD8")
{
}
public DbSet<Osoba> Osoba { get; set; }
public DbSet<Klient> Klient { get; set; }
public DbSet<Pracownik> Pracownik { get; set; }
public DbSet<Nieruchomosc> Nieruchomosc { get; set; }
public DbSet<Biuro> Biuro { get; set; }
public DbSet<Dom> Dom { get; set; }
public DbSet<Grunt> Grunt { get; set; }
public DbSet<Hala> Hala { get; set; }
public DbSet<Mieszkanie> Mieszkanie { get; set; }
public DbSet<Spotkanie> Spotkanie { get; set; }
public DbSet<Umowa> Umowa { get; set; }
}
And finally here is how I'm adding new Klient to database:
private void KlientAdd_Click(object sender, RoutedEventArgs e)
{
using (var ctx = new BazyDanychContext())
{
Klient tmp = new Klient { Imie = KlientImie.Text, Nazwisko = KlientNazwisko.Text, Telefon = KlientTelefon.Text, Adres = KlientAdres.Text, Mail = KlientMail.Text, IloscTransakcji = Int32.Parse(KlientIloscTransakcji.Text), Typ = "Klient" , Pracownik = KlientPracownik.Text, Sprzedane = Int32.Parse(KlientSprzedane.Text), Kupione = Int32.Parse(KlientKupione.Text), Preferencje = KlientPreferencje.Text };
ctx.Osoba.Add(tmp);
ctx.SaveChanges();
}
InitTabs();
}
So, final solution for me was removing all migrations in my project. After dropping my Database, removing all migrations and then recreate Database without any migrations in my project it finally worked. Thank you for all your suggestions.
I have table Player and table Statistic and other tables that are not important in this question.
Table Player has PK Player_ID and it is FK in table Statistic. The relationship between these tables is one-to-many (one player can have more statistics).
Here is the code:
GenericRepository(I have created it to have unique class for CRUD methods)
public async Task<int> Delete<T>(Guid id) where T : class
{
try
{
T entity = await Get<T>(id);
Context.Set<T>().Remove(entity);
return await Context.SaveChangesAsync();
}
catch (Exception ex)
{
throw ex;
}
}
PlayerRepository(for managing operations on Player table)
public async Task<int> Delete(Guid id)
{
try
{
var player = await GenRepository.Get<Player>(id);
if (player == null)
{
return 404;
}
else
{
return await GenRepository.Delete(player);
}
}
catch (Exception ex)
{
throw ex;
}
}
PlayerService(connection between repository and controller in WebAPI)
public async Task<int> Delete(Guid id)
{
try
{
return await PlayerRepository.Delete(id);
}
catch (Exception ex)
{
throw ex;
}
}
PlayerController
[HttpDelete]
[Route("deleteplayer")]
public async Task<HttpResponseMessage> Delete(Guid id)
{
try
{
var Finder = Mapper.Map<PlayerView>(await PlayerService.Get(id));
if(Finder == null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Player doesn't exist in database.");
}
var Response = await PlayerService.Delete(id);
var profile = "../../../app/pictures/FootballFanAppPictures/" + Finder.Club_ID.ToString().ToUpper() + "/profiles/" + id.ToString().ToUpper() + ".png";
var details = "../../../app/pictures/FootballFanAppPictures/" + Finder.Club_ID.ToString().ToUpper() + "/" + id.ToString().ToUpper() + ".png";
if (System.IO.File.Exists(profile))
{
System.IO.File.Delete(profile);
}
if (System.IO.File.Exists(details))
{
System.IO.File.Delete(details);
}
return Request.CreateResponse(HttpStatusCode.OK, Response);
}
catch(Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
}
Entity models:
-database models
public partial class Player
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Player()
{
this.Statistic = new HashSet<Statistic>();
}
public System.Guid Player_ID { get; set; }
public System.Guid Club_ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public double Height { get; set; }
public int Weight { get; set; }
public System.DateTime BirthDate { get; set; }
public string Nationality { get; set; }
public string Position { get; set; }
public int Shirtnmbr { get; set; }
public virtual Club Club { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Statistic> Statistic { get; set; }
}
using System;
using System.Collections.Generic;
public partial class Statistic
{
public System.Guid Statistic_ID { get; set; }
public System.Guid Player_ID { get; set; }
public int Goals { get; set; }
public int Assists { get; set; }
public int FoulsFor { get; set; }
public int FoulsAgainst { get; set; }
public int ShotsTotal { get; set; }
public int ShotsGoal { get; set; }
public virtual Player Player { get; set; }
}
-domain models (used in repository)
public class PlayerDomain : IPlayerDomain
{
public Guid Player_ID { get; set; }
public Guid Club_ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public double Height { get; set; }
public int Weight { get; set; }
public DateTime BirthDate { get; set; }
public string Nationality { get; set; }
public string Position { get; set; }
public int Shirtnmbr { get; set; }
public virtual ICollection<IStatisticDomain> Statistic { get; set; }
}
public class StatisticDomain: IStatisticDomain
{
public Guid Statistic_ID { get; set; }
public Guid Player_ID { get; set; }
public int Goals { get; set; }
public int Assists { get; set; }
public int FoulsFor { get; set; }
public int FoulsAgainst { get; set; }
public int ShotsTotal { get; set; }
public int ShotsGoal { get; set; }
}
-view models (used in controller)
public class PlayerView
{
public Guid Player_ID { get; set; }
public Guid Club_ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public double Height { get; set; }
public int Weight { get; set; }
public DateTime BirthDate { get; set; }
public string Nationality { get; set; }
public string Position { get; set; }
public int Shirtnmbr { get; set; }
public virtual ICollection<StatisticView> Statistic { get; set; }
}
public class StatisticView
{
public Guid Statistic_ID { get; set; }
public Guid Player_ID { get; set; }
public int Goals { get; set; }
public int Assists { get; set; }
public int FoulsFor { get; set; }
public int FoulsAgainst { get; set; }
public int ShotsTotal { get; set; }
public int ShotsGoal { get; set; }
}
Every class is in a separate file. I use database first approach so i got .edmx file along with database models. Database is created in SQL Server Management Studio.
I can update Player but when I try to delete it i get this error:
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.
I have searched various answers on google and stackoverflow but I couldn't find an answer that solves my problem
Before you call this line:
var Response = await PlayerService.Delete(id);
You are going to need to retrieve a list of your statistics that are assigned to the player you are trying to delete.
Then loop trough each of the statistics and delete those from your database first.
var stats = Finder.Statistic.ToList();
if(stats != null && stats.Any())
{
foreach(var stat in stats)
{
//retrieve your stat record here from the database
//check that the stat record is not null
//delete your stat record here
}
}
Then you should be able to delete your player record as there will no longer be references to it.
OR
You could just set ON DELETE CASCADE to true, but you need to be careful that you fully understand what all will be deleted on player deletion.
I don't see Statistic deletion on Player delete in your code.
Before you can delete a Player, you need to delete all Player related Statistics first. If there is any left, a player will fail on delete operation.
Set PrimaryKey and Foreignkey Relationship :according to #Bad Dub suggestion.
I am working on Entity Framework 6 and repositories setup to do crud operations. I am trying to insert record in one of the table and getting error for null entries even duo it is not.
{"Cannot insert the value NULL into column 'UI_ID', table 'Blackpool_BPM.dbo.COURSE_INSTANCE'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
The connection to database is correct as I can read data with no problem
At Generic Repository, getting data correctly, just before Save()
Table Structure
Class Model
[Table("COURSE_INSTANCE")]
public class BIZCourseInstanceEntity
{
[Key]
public int UI_ID { get; set; }
public string UnitInstanceCode { get; set; }
public string FESLongDescription { get; set; }
public string FESShortDescription { get; set; }
public string FullDescription { get; set; }
public string OwningOrganisationCode { get; set; }
public int? OwningOrganisationID { get; set; }
public string TopicCode { get; set; }
public string UnitCategory { get; set; }
public string UnitCode { get; set; }
public string FESQualificationType { get; set; }
public int? SCHOOLS { get; set; }
public int? MARKETING_GROUPS { get; set; }
}
Repository
public class BIZCourseInstanceRepository : GenericRepository<BIZCourseInstanceEntity>
{
public BIZCourseInstanceRepository() { }
public BIZCourseInstanceRepository(DbContext dbContext)
:base(dbContext)
{ }
}
Unit of work class
public class BIZ_UOF : IDisposable
{
private BIZDbContext _BIZDbContextObject = new BIZDbContext();
protected BIZCourseInstanceRepository _BIZCourseInstanceRepository;
public BIZCourseInstanceRepository BIZCourseInstanceRepository
{
get
{
if (this._BIZCourseInstanceRepository == null)
{
this._BIZCourseInstanceRepository = new BIZCourseInstanceRepository(_BIZDbContextObject);
}
return _BIZCourseInstanceRepository;
}
}
/////
public void Save()
{
_BIZDbContextObject.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
_BIZDbContextObject.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_BIZDbContextObject.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
DbContext
public class BIZDbContext : BaseContext<BIZDbContext>
{
public BIZDbContext() : base("_DbContext")
{ }
public DbSet<BIZCourseInstanceEntity> BIZ_CourseInstance { get; set; }
}
Generic Repository CRUD
public void InsertEntity(TEntity obj)
{
_DbSet.Add(obj);
}
Function class where Error is generating at Save()
public void InsertCourseInstance()
{
BIZCourseInstanceEntity BIZCourseInstanceEntityObject = null;
BIZCourseInstanceEntityObject = new BIZCourseInstanceEntity
{
UI_ID = 999999,
UnitInstanceCode = "KZ999999",
FESLongDescription = "LONG",
FESShortDescription = "SHORT",
FullDescription = "FULL",
OwningOrganisationCode = "E",
OwningOrganisationID = 155,
TopicCode = "04.1",
UnitCategory = "04",
UnitCode = "HE-G",
FESQualificationType = null,
SCHOOLS = 5,
MARKETING_GROUPS = 44
};
using (var _uow = new BIZ_UOF())
{
_uow.BIZCourseInstanceRepository.InsertEntity(BIZCourseInstanceEntityObject);
_uow.Save();
}
}
You need to tell Entity Framework that your ID is an identity field. If it isn't set up as that in the database, then you need to do that. Otherwise, you'll need to query for the next available ID, and then hope you don't collide with another request trying to save something at the same time.
[Table("COURSE_INSTANCE")]
public class BIZCourseInstanceEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UI_ID { get; set; }
...
}
If you absolutely have to work without any sort of database generated options for your Primary Key, you can instead use the DatabaseGeneratedOption.None value of the enum. This should be avoided to prevent collisions on your PK, but the option does exist.
I have found answer, the problem was in my database I am require to provide the Primary key which in my case is UI_ID but in my model I haven't define DatabaseGeneredOption.None, hence throwing error, Thanks for Krillgar guiding me
here is updated model
[Table("COURSE_INSTANCE")]
public class BIZCourseInstanceEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UI_ID { get; set; }
[StringLength(255)]
public string UnitInstanceCode { get; set; }
[StringLength(255)]
public string FESLongDescription { get; set; }
[StringLength(255)]
public string FESShortDescription { get; set; }
[StringLength(255)]
public string FullDescription { get; set; }
[StringLength(255)]
public string OwningOrganisationCode { get; set; }
public int? OwningOrganisationID { get; set; }
[StringLength(255)]
public string TopicCode { get; set; }
[StringLength(255)]
public string UnitCategory { get; set; }
[StringLength(255)]
public string UnitCode { get; set; }
[StringLength(50)]
public string FESQualificationType { get; set; }
public int? SCHOOLS { get; set; }
public int? MARKETING_GROUPS { get; set; }
}
Pretty new to ASP.NET and programming. I have two models, two API controllers, two repositories. How do I post the data to the second model while attaching it to the first (I'm guessing by ID.) Do I possibly need a View Model? Also reading a little about unit of work. Maybe neither are necessary? Below is some code. Thanks!
Record.cs
namespace Train.Models {
public class Record {
public int Id { get; set; }
public int Quantity { get; set; }
public DateTime DateCreated { get; set; }
public bool IsActive { get; set; }
public string UserId { get; set; }
public virtual ICollection<Cars> Cars { get; set; }
}
}
Cars.cs
namespace Train.Models {
public class Cars {
public int Id { get; set; }
public string EmptyOrLoaded { get; set; }
public string CarType { get; set; }
//Hopper, flatbed, tank, gondola, etc.
public string ShippedBy { get; set; }
//UP(Union Pacific) or BNSF
public string RailcarNumber { get; set; }
//public virtual ApplicationUser ApplicationUser { get; set; }
public string UserId { get; set; }
public string RecordId { get; set; }
public virtual Record Record { get; set; }
}
}
Record Repository
public void SaveRecord(Record recordToSave) {
if (recordToSave.Id == 0) {
recordToSave.DateCreated = DateTime.Now;
_db.Record.Add(recordToSave);
_db.SaveChanges();
} else {
var original = this._db.Record.Find(recordToSave.Id);
original.Quantity = recordToSave.Quantity;
original.IsActive = true;
_db.SaveChanges();
}
}
EFRepository (Cars)
public void SaveCar(Cars carToSave) {
if (carToSave.Id == 0) {
_db.Cars.Add(carToSave);
_db.SaveChanges();
} else {
var original = this.Find(carToSave.Id);
original.EmptyOrLoaded = carToSave.EmptyOrLoaded;
original.CarType = carToSave.CarType;
original.ShippedBy = carToSave.ShippedBy;
original.RailcarNumber = carToSave.RailcarNumber;
_db.SaveChanges();
}
}
I'm very new to MVC/LINQ so bear with me. I'm wanting to check if that TransmissionStatus var is false and if so linq to db to pull the appropriate failed message.
class AuditLogManagement
{
public int ID { get; set; }
public DateTime Date { get; set; }
public string MsgType { get; set; } //possibly enum?
public AuditLogEventDetails EventDetails { get; set; }
public string MsgContents { get; set; }
public bool TransmissionStatus { get; set; }
public string FailedTransmissionMsg
{
get
{
if (!TransmissionStatus)
{
//linq to db to return failed xmission message
}
}
}
}