I am trying to add a new record to a table i my database. The code I have adds the record to the table but also throws an exception "System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_COURSE_TAKEN'. Cannot insert duplicate key in object 'dbo.COURSE_TAKEN'. The duplicate key value is (000160228, HIS 155).". Why is it trying to add the record twice? I know for 100% certainty that there is no existing record already present before I try to add it. Below is my code for adding the record:
using (KuPlanEntities db = new KuPlanEntities())
{
var courseTake = new COURSE_TAKEN();
courseTake.Id = "000160228";
courseTake.courseAbNum = courseAbNum;
courseTake.status = status;
courseTake.grade = grade;
db.COURSE_TAKEN.Add(courseTake);
db.SaveChanges();
}
Related
def saveTldAndUpdateTimestamp(EntityTld entityTld) {
log.debug("EntityTldService saveTldAndUpdateTimestamp Start")
entityTld.lastUpdated = new Date()
if (!entityTld.dateCreated) {
entityTld.dateCreated = entityTld.lastUpdated
}
EntityTld savedEntityTld = entityTld.save(flush: true, failOnErrors: true)
log.debug("EntityTldService saveTldAndUpdateTimestamp Exit Entity ID:: ${savedEntityTld?.id}")
return savedEntityTld
}
Example : savedEntityTld with id - 200 in the database.
However in the log statement, instead of printing 200, it prints 201 and the rest of the values is similar to the record of id = 200.
FYI, the table corresponding to the domain EntityTld has a GENERATED BY DEFAULT AS IDENTITY column for the id.
The main objective here is, after saving the entitytld record into the database table, i need the saved id and will insert a new record into table2 (this id being one of the columns in table2)
I have table ProjekRumah and ProjekKMDetails.
When I use entities.SaveChanges() on ProjekRumah data, new row with ProjectID will yield its Identity. Suppose that number is 7.
Then I take this ProjekID=7, to create ProjekKMDetails data. Then, entities.SaveChanges(). Repeat same step Table1, Table2, Table3, Table 4 and so on with ProjectID=7 as Foreign Key.
All is fine on sunny day. What if ProjekID=7 already exists in one of these table, suppose hypothetically in Table3.
I understand this is not suppose to happen. But suppose its Friday the 13th an intern might drop all constraints and forgot to truncate Table3. Or some evil data corruption.
using (MyDBEntities entities = new MyDBEntities())
{
using (var dbContextTransaction = entities.Database.BeginTransaction())
{
try
{
var dataRumah = new ProjekRumah
{
ProjekStatus = 'SpecialScenario',
ProjekName = data.ProjekName, //data passed in from UI
};
entities.ProjekRumah.Add(dataRumah);
entities.SaveChanges(); // the first .SaveChanges to acquire Identity increment
var dataKMDetails = new ProjekKMDetails
{
ProjekID = dataRumah.ProjekID, //identity ID
KMJPBDNo = data.KMJPBDNumber, //data passed in from UI
};
entities.ProjekKMDetails.Add(dataKMDetails);
var table3Details = new Table3
{
ProjekID = dataRumah.ProjekID, //identity ID
Table3Stuff = data.ForTable3Stuff, //data passed in from UI
};
entities.Table3.Add(table3Details );
//..... No .SaveChanges anywhere
//..... more tables, Tables 4, 5 6 etc
//..... No round trips to database
entities.SaveChanges(); //the second and final .SaveChanges()
dbContextTransaction.Commit();
}
catch (Exception ex)
{
dbContextTransaction.Rollback();
}
}
My intent is to be defensive and safe side, is there a syntax to check if Table3 already have row with ProjectID = 7?
To my understanding attempting to delete anything from Table3 will require delete on same corresponding ID in ProjekRumah and ProjekKMDetails and Table4 and so on:
ProjekRumah kmContext = new ProjekRumah {
ProjekID = 7,
ProjekKMDetails = new ProjekKMDetails { ProjekID = 7 },
Table3= new Table3 { ProjekID = 7 }
};
entities.ProjekRumah.Attach(kmContext);
entities.ProjekRumah.Remove(kmContext);
entities.SaveChanges(); //this actually deletes from multiple tables
But at this stage there is no (should be) such record in ProjekRumah table because earlier on: entities.Table3.Add(dataTable3Details) would be illegal and caused rollback.
How do I write code to be on defensive side in case 'Foreign Key Already Exist' happens?
Or such a scenario can never ever 101% to happen?
If possible, I would like to cleanse with EF functions as succinct and as elegant as possible.
PS: I guess I can summarize my question as this:
Suppose an orphan row was found despite Foreign Key constrains (should never happen I believe), but how do I use EF to handle this in a single transaction (preferably with no round trips such as Count, Select, FirstorDefault etc)?
I am trying to extract the [key] value from a table.
This is for a logging method which looks like this:
private List<Log> GetAuditRecordsForChange(DbEntityEntry dbEntry, string userId)
{
List<Log> result = new List<Log>();
DateTime changeTime = DateTime.Now;
// Get the Table() attribute, if one exists
TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
// Get table name (if it has a Table attribute, use that, otherwise get the pluralized name)
string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;
// Get primary key value
string keyName = dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;
if (dbEntry.State == EntityState.Added)
{
result.Add(new Log()
{
LogID = Guid.NewGuid(),
EventType = "A", // Added
TableName = tableName,
RecordID = dbEntry.CurrentValues.GetValue<object>(keyName).ToString(),
ColumnName = "*ALL",
NewValue = (dbEntry.CurrentValues.ToObject() is IDescribableEntity) ? (dbEntry.CurrentValues.ToObject() as IDescribableEntity).Describe() : dbEntry.CurrentValues.ToObject().ToString(),
Created_by = userId,
Created_date = changeTime
}
);
}
The problem is to get the RecordID when a Record is added, when it get deleted or modified it works. (The code to get it is the same)
When I debug I also see that it has the KeyAttribute in the CustomAttributes base but not sure why it always shows up as 0.
I can debug more if needed
After savechanges you can fetch the newly created key. (Guess the key is generated automatically inserting a new record).
for me you have several solutions.
first solution:
enlist added entity from the context
SaveChanges
enumerate the enlisted entities to add log
SaveChanges
the problem (or not) here is that the business and the logging are not in the same transaction.
antother problem, depending on the implementation, is to prevent loging of log of log of log... This can be donne by filtering entities by typeName for example.
other solution:
add and ICollection<Log> to your entities.
the problem here is to unify the logs:
inheritance of entity, or
several log tables + a view
...
other solution
use trigger at database level
other solution
..., use cdc if you have Sql Server Enterprise Edition
I am calling a web service from my MVC project and if it is successful then it returns process complete. This result, I am storing in variable called y.
var y = Here pass required parameters and if it is successfull store result in y
when I put breakpoint here and if process complete, I can see result in var y.
So if process complete I need to update my table. For this can I do like this ?
if( y = "Process complete")
{
update table code here
}
and I don't know how to update table in Entity Framework. Here I need to update table called table1 and set column2 = 1, column 3 = value of column 4 where column 1 = value of column 1.
What I know for this is :
UPDATE tableName
SET column2 = 1, column3 = context.FirstOrDefault().column4
WHERE column1 = context.FirstOrDefault(). column1
Update :
Hi i got to know how to write code to update table.But when i put break-point and come to savechanges method i am getting Property export is part of the objects key information and cannot be modified error.
This is the code i am using to update my table :
var rec = (from s in geton.table_1
where s.on_id == geton.table_1.FirstOrDefault().on_id
select s).FirstOrDefault();
rec.export = 1;
rec.on_date = geton.table_1.FirstOrDefault().on_date;
geton.SaveChanges();
A new entity can be added to the context by calling the Add method on DbSet. This puts the entity into the Added state, meaning that it will be inserted into the database the next time that SaveChanges is called.
For example:
using (var context = new YourContext())
{
var record = new TypeName { PropertyName = "Value" };
context.EntityName.Add(record );
context.SaveChanges();
}
For More Info :
http://msdn.microsoft.com/en-us/library/bb336792.aspx
http://msdn.microsoft.com/en-us/data/jj592676.aspx
http://www.entityframeworktutorial.net/significance-of-savechanges.aspx
Hi i got to know how to write code to update table.But when i put break-point and come to savechanges method i am getting Property export is part of the objects key information and cannot be modified error.
That sounds more like a Key error. Are you sure you have put a primary key on that table?
If not then EF just uses the whole table as the key essentially
I'm using Entity Framework 4.1
I have a "DomainEntities" table that holds the common info for all my domain entities.
I have a users table the the UserID is a Foreign Key from "DomainEntities".
see EDMX:
When I run the following code i get an error:
Unable to determine a valid ordering for dependent operations.
Dependencies may exist due to foreign key constraints, model
requirements, or store-generated values.
The code:
static void addUserTest()
{
DomainEntity userToAdd = new DomainEntity()
{
EntityName = "Test User",
EntityTypeID = DomainEntity.eEntityType.User,
EntityCreationDate = new DateTime(),
EntityLastUpdateDate = new DateTime(),
EntityCreatorUserID = 0,
EntityUpdaterUserID = 0,
EntityParentID = null,
UserDetails = new User()
{
Username = "TestUser",
Password = "123",
FirstName = "Test",
LastName = "User"
}
};
using (var context = new CamelotDB())
{
context.DomainEntities.Add(userToAdd);
context.SaveChanges();
}
}
I cant understand what is the reason that EF can understand what is the INSERT order required,
It should be One record into "DomainEntities" and then one record into "Users".
What am I doing wrong ?
After searching for one more day I found it the problem was with the Creator and Updater self referenced foreign keys.
CreatorID is not Nullable so does UpdaterID and this is why EF requires the navigation properties to point to actual entities from the database so i added the following lines in the initializer of Test User.
EntityCreatorUserID = 0,
Creator = context.DomainEntities.Find(0),
EntityUpdaterUserID = 0,
Updater = context.DomainEntities.Find(0),
It seems that instead of having your User be related to your DomainEntity, you should make your User a subclass of DomainEntity. In the Entity Model designer, this is done by using the Inheritance tool (Double-click the Inheritance tool in the toolbox, then click once on the parent entity and once on the child entity.)
This more accurately describes the nature of a User; a User is a DomainEntity. Your current model, suggests that a User is related to a DomainEntity, which doesn't seem right.