Hey. Nooby question but new with Entity.
I am trying to create a new user object and some details in some additional tables which have a foreign key of the newly created user id.
I've attempted to do this in one round trip. Do I have to add the user to the database first and then go back, set the userid's on the other objects and add them. Here is the Entity Model and code to elaborate:
using (var context = new ServicesEntities())
{
newUser = new UsersTable();
newUser.Address = user.UserDetails.Address;
newUser.City = user.UserDetails.City;
newUser.Country = user.UserDetails.Country;
newUser.Email = user.Email.EmailString;
newUser.FirstName = user.UserDetails.FirstName;
newUser.LastName = user.UserDetails.LastName;
newUser.State = user.UserDetails.State;
newUser.Zip = user.UserDetails.Zip;
context.UsersTables.AddObject(newUser);
context.SaveChanges();
}
using (var context = new ServicesEntities())
{
var referralDetails = new UserReferrals();
referralDetails.CreatedThruServiceId = 1; // todo don't make this an absolute 1
referralDetails.ReferralEmail = user.ReferralDetails.ReferralEmail;
referralDetails.TwoPlusTwoHandle = user.ReferralDetails.TwoPlusTwoHandle;
referralDetails.UserId = newUser.UserId;
context.UserReferrals.AddObject(referralDetails);
context.SaveChanges(); // THIS WORKS FINE!
}
using (var context = new ServicesEntities())
{
var credentials = new UserCredentials();
credentials.CreatedOn = DateTime.Now;
credentials.EmailValidated = false;
//credentials.EmailValidatedOn = null;
credentials.FailedLoginAttempts = 0;
credentials.IsLockedOut = false;
//credentials.LastValidLogin = null;
credentials.Password = user.Password.PasswordString;
credentials.PermissionId = 1; // todo don't make this an absolute 1 = user
credentials.SuccessfulLoginAttempts = 0;
credentials.UserId = newUser.UserId; ;
context.UserCredentials.AddObject(credentials);
context.SaveChanges(); // THIS ONE CRAPS OUT!
}
When I run this I get the following Exception when I run SaveChanges():
{"A dependent property in a
ReferentialConstraint is mapped to a
store-generated column. Column:
'UserId'."}
Note: Updated this with some slightly different code based on an example in a book.
Note2: I've narrowed down the problem to be in the adding of credentials.
Note3: Fixed this, I accidentally had AUTO-INCREMENT set on my UserCredentials userid. If anyone ares here is working code:
public POCO.User AddNewUserToDb(User user)
{
if (IsDuplicateUser(user.Email.EmailString))
{
throw new DuplicateNameException("This email is already taken.");
}
UsersTable newUser;
using (var context = new ServicesEntities())
{
newUser = new UsersTable();
newUser.Address = user.UserDetails.Address;
newUser.City = user.UserDetails.City;
newUser.Country = user.UserDetails.Country;
newUser.Email = user.Email.EmailString;
newUser.FirstName = user.UserDetails.FirstName;
newUser.LastName = user.UserDetails.LastName;
newUser.State = user.UserDetails.State;
newUser.Zip = user.UserDetails.Zip;
var referralDetails = new UserReferrals();
referralDetails.CreatedThruServiceId = 1; // todo don't make this an absolute 1
referralDetails.ReferralEmail = user.ReferralDetails.ReferralEmail;
referralDetails.TwoPlusTwoHandle = user.ReferralDetails.TwoPlusTwoHandle;
//referralDetails.UserId = newUser.UserId;
var credentials = new UserCredentials();
credentials.CreatedOn = DateTime.Now;
credentials.EmailValidated = false;
//credentials.EmailValidatedOn = null;
credentials.FailedLoginAttempts = 0;
credentials.IsLockedOut = false;
//credentials.LastValidLogin = null;
credentials.Password = user.Password.PasswordString;
credentials.PermissionId = 1; // todo don't make this an absolute 1 = user
credentials.SuccessfulLoginAttempts = 0;
//credentials.UserId = newUser.UserId; ;
newUser.Credentials = credentials;
newUser.ReferralDetails = referralDetails;
context.UsersTables.AddObject(newUser);
context.SaveChanges();
}
user.UserId = newUser.UserId;
return user;
Try to add related records directly to UserTable record:
newUser.Credentials.Add(credentials);
newUser.ReferralDetails.Add(referralDetails);
Do not set any Id. It will be set during saving automatically.
Edit: Btw. make sure that UserId column in UserCredentials table is not set as auto generated in database.
Take a look at these links:
Using the entity framework to add existing entities to a collection on a newly created entity.
How to create foreign key relationships with the Entity Framework?
Entity Framework - insert new object with a collection of existing objects
The key links for EF4:
Fortunately, in EF4 we can directly
update the relationship due to the
Foreign Key Association which allows
us to keep the foreign key property
inside the entity classes. For
detail, please see
http://blogs.msdn.com/adonet/archive/2009/11/06/foreign-key-relationships-in-the-entity-framework.aspx.
Besides, we have another great feature
Self Tracing Entities, which makes the
n-tier pattern much easier in EF,
http://blogs.msdn.com/adonet/archive/2009/11/15/updated-feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx.
EF4 will allow you to include the foreign key values as scalar properties of your entities. Make sure the "Include foreign keys" checkbox is checked when you create your EDM.
Looking at your model, it looks like you've already done that. Just set the foreign key values explicitly, no need to do a round trip.
Related
I have a scenario where I need to insert a record into a table - the myPerson Entity to get the personID (int identity). Then I need to take that personID and add it to the familyAdmin.personID.
I was hoping to do all of this in one db.SaveChange();
Is there a way to do it in one since the person entity is part of the userAdmin entity?
Currently how I have it set up:
userAdmin familyAdmin = new userAdmin();
familyAdmin.signupDate = DateTime.Today;
familyAdmin.lastLogin = DateTime.Today;
familyAdmin.registrationID = newReg.registrationID;
person myPerson = new person();
myPerson.personName = newReg.name;
myPerson.personEmail = newReg.email;
myPerson.personPhone = newReg.phone;
myPerson.personPhoneCarrier = newReg.carrierName;
myPerson.personDOB = newReg.dob;
myPerson.familyID = familyAdmin.familyID;
myPerson.personTypeID = newReg.personTypeID;
familyAdmin.personID = myPerson.personID;
familyAdmin.people.Add(myPerson);
db.userAdmins.Add(familyAdmin);
db.SaveChanges();
This option is currently not working. There is no error that is generated when I do this option. I just get a 0 instead of a 1,2,3 etc.
The insert into the person table is working and an ID is being generated correctly. It is just when I try to retrieve that ID to do the insert into the UserAdmin table at the same time that I get the 0.
Do I have to do something like this:
person myPerson = new person();
myPerson.personName = newReg.name;
myPerson.personEmail = newReg.email;
myPerson.personPhone = newReg.phone;
myPerson.personPhoneCarrier = newReg.carrierName;
myPerson.personDOB = newReg.dob;
myPerson.familyID = familyAdmin.familyID;
myPerson.personTypeID = newReg.personTypeID;
db.person.Add(myPerson);
db.SaveChanges();
userAdmin familyAdmin = new userAdmin();
familyAdmin.signupDate = DateTime.Today;
familyAdmin.lastLogin = DateTime.Today;
familyAdmin.registrationID = newReg.registrationID;
familyAdmin.personID = myPerson.personID;
db.userAdmins.Add(familyAdmin);
db.SaveChanges();
Partial EDMX image:
The first option is not working because of these two lines:
myPerson.familyID = familyAdmin.familyID;
familyAdmin.personID = myPerson.personID;
Those lines are not necessary because you're checking identifiers that are not created in the database.
Your code should look like this:
userAdmin familyAdmin = new userAdmin();
familyAdmin.signupDate = DateTime.Today;
familyAdmin.lastLogin = DateTime.Today;
familyAdmin.registrationID = newReg.registrationID;
person myPerson = new person();
myPerson.personName = newReg.name;
myPerson.personEmail = newReg.email;
myPerson.personPhone = newReg.phone;
myPerson.personPhoneCarrier = newReg.carrierName;
myPerson.personDOB = newReg.dob;
// myPerson.familyID = familyAdmin.familyID; Not necessary
myPerson.personTypeID = newReg.personTypeID;
// familyAdmin.personID = myPerson.personID; Not necessary
familyAdmin.people.Add(myPerson);
db.userAdmins.Add(familyAdmin);
db.SaveChanges();
If you have correctly configured the relation between people property on userAdmin entity and person entity so by using familyAdmin.people.Add(myPerson); and calling db.SaveChanges(); then EF will call DetectChanges internally so that will fix the relationship between those two entities and generate the correct SQL in the correct order because it knows which entity need to be inserted first and which entity need the foreign key id.
I have created new entity.
From that entity i call Custom Workflow Activity entity that creates opportunity.
It works, but additionally I have to change some fields on created opportunity.
(I have to add opportunity products, and have to change price list for each opportunity).
As a test I tried to Update account field after creation, but it failed field. When i populate this account field before creation, it works, so it is not about that.
Here is the part of the code:
Entity entity = null;
if (context.InputParameters != null && context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
}
else
{
entity = service.Retrieve(context.PrimaryEntityName, ((Guid)context.PrimaryEntityId), new ColumnSet(true));
}
Entity opportunity = new Entity("opportunity");
string name = entity.GetAttributeValue<string>("subject");
opportunity["name"] = name;
opportunityId = service.Create(opportunity);
EntityReference accountlookup = (EntityReference)entity.Attributes["ad_sendto"];
Guid accountId = accountlookup.Id;
opportunity["parentaccountid"] = new EntityReference("account", accountId);
service.Update(opportunity);
To repeat, it creates opportunity, but it doesn't work for update, is there any other way to do this, or do I have some errors here?
It fails because you are trying to update opportunity entity which does not have a primary key (opportunityid) set.
Instead of updating the opportunity after it was created, why not just assign the parentaccountid during the create operation?
var opportunity = new Entity("opportunity");
opportunity["name"] = entity.GetAttributeValue<string>("subject"); ;
opportunity["parentaccountid"] = entity.Attributes["ad_sendto"];
opportunityId = service.Create(opportunity);
For future references, if you ever have to update an entity that was just created or any entity for that matter:
var opportunityToUpdate = new Entity("opportunity")
{
Id = opportunityId
};
opportunityToUpdate["parentaccountid"] = entity.Attributes["ad_sendto"];
service.Update(opportunityToUpdate);
How I can do just this ( a.myFavorits.Add()) without pulling the all object to var a , because a has a lot of data, and I don't want to pull all a object, but I can't find a way do do it.
I want to do the lambada and the linq without return something but linq is always return something
public static void addFavorits(long f,long idUser)
{
using (var db = dataBase())
{
// here i pull object user from users table
var a = db.users.Where(c => c.id == idUser).SingleOrDefault();
// here i adding to the object field myFavorits new value
//myFavorits is also a table of entitys that connected to user object
a.myFavorits.Add(new BE.FavoritsUsersLong { myLong = f });
db.SaveChanges();
}
}
I thought to do something like this but i dont know how to set the field users_TableId that is the key that connect the 2 tables
public static void addFavorits(long favoritId,long idUser)
{
using (var db = dataBase())
{
db.favoritsUsersLong.Add(new BE.FavoritsUsersLong {myLong = favoritId}
/*,users_TableId =idUser*/);
db.SaveChanges();
}
}
Here's a concrete example that does what you want. In this example, only the Name of a Company is modified and saved. Or an item is added to one of its collections.
var cmp = new Company{ CmpId = 1, Name = "Cmp1" }; // CmpId is the primary key
db.Companies.Attach(cmp);
db.Entry(cmp).Property(c => c.Name).IsModified = true;
// Or add an entity to a collection:
cmp.Users = new[] {new User { Name = "a1", PassWord = "a1" } };
try
{
db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();
}
finally
{
db.Configuration.ValidateOnSaveEnabled = true;
}
Result in SQL:
DECLARE #0 VarChar(30) = 'Cmp1'
DECLARE #1 Int = 1
UPDATE [dbo].[Company]
SET [Name] = #0
WHERE ([CmpId] = #1)
There are a few things to note here:
Obviously you need to know the Id of the entity you want to modify.
The object you create is called a stub entity, which is an incomplete entity. When you try to save such an entity, EF is very likely to complain about null values in required properties. That's why almost certain you'd have to disable validation (temporarily, or, better, dispose the context immediately).
If you want to add an item to a collection, you should leave validation enabled, because you'd want to know for sure that the new entity is valid. So you shouldn't mix these two ways to use a stub entity.
If you often need roughly the same small part of your entity you may consider table splitting.
I'm guessing this is what you want? I don't see you 'editting' I only see you adding.
using (var db = dataBase())
{
var a = new user();
....
//set properties etc..
...
a.myFavorits.Add(new BE.FavoritsUsersLong { myLong = f });
db.users.Add(a);
db.SaveChanges();
}
I have an issue that I am trying to resolve where I have a relationship between two tables. As soon as I save a record to the 1st Table I then want to read that same record back so I can get the primary key and assign it to my next table.
Code I have written is below
public IEnumerable<CombinedPayCode> ImportCombinedPayCodes()
{
var xmlFile = Path.Combine(_xmlPath, "WSACombinedPayCodeRule.xml");
var stream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read);
var xdoc = XDocument.Load(stream);
var combinedPayCodeCollection = xdoc.Descendants("WSACombinedPayCodeRule");
var Collection = new List<CombinedPayCode>();
foreach (var element in combinedPayCodeCollection)
{
var combinedPayCode = new CombinedPayCode
{
Name = (string)element.Attribute("Name"),
AmountType = (string)element.Attribute("AmountType"),
VisibleInReport = (bool)element.Attribute("VisibleInReport"),
MoneyCategory = (bool)element.Attribute("MoneyCategory"),
VisibleToUser = (bool)element.Attribute("VisibleToUser"),
CustomerId = 11,
};
_db.CombinedPayCodes.Add(combinedPayCode);
// I want to obtain the Primary Key of the above record once saved to database
var payCodeNodes = element.Descendants("SimpleValue");
foreach (var selectedPayCode in payCodeNodes)
{
var combinedPayCodeList = new CombinedPayCodeList
{
PayCodeId = selectedPayCode.FirstAttribute.Value,
// I want to replace the next line of code to assign the
// foreign key as per the primary key of the record above
CombinedPayCodeId = 1,
};
_db.CombinedPayCodeLists.Add(combinedPayCodeList);
}
_db.SaveChanges();
Collection.Add(combinedPayCode);
}
return Collection;
}
Is the primary key of CombinedPayCode an identity column? If so, just reference combinedPayCode.ThePrimaryKeyColumn in your code.
If CombinedPayCodeList is a collection of something that hangs off CombinedPayCode you can save yourself some effort and just build the object graph and add the parent object then SubmitChanges(). EF will take care of transferring the primary key into the child entity and wrap everything in a nice neat transaction for you.
UPDATE: Something like this. I may have some of your object/property names wrong.
foreach( var element in combinedPayCodeCollection )
{
// Create the CombinedPayCode object
var combinedPayCode = new CombinedPayCode
{
Name = (string)element.Attribute("Name"),
AmountType = (string)element.Attribute("AmountType"),
VisibleInReport = (bool)element.Attribute("VisibleInReport"),
MoneyCategory = (bool)element.Attribute("MoneyCategory"),
VisibleToUser = (bool)element.Attribute("VisibleToUser"),
CustomerId = 11,
};
var payCodeNodes = element.Descendants("SimpleValue");
foreach( var selectedPayCode in payCodeNodes )
{
var combinedPayCodeList = new CombinedPayCodeList
{
PayCodeId = selectedPayCode.FirstAttribute.Value,
};
// Add each PayCodeNode to the CombinedPayCode
combinedPayCode.CombinedPayCodeLists.Add( combinedPayCodeList );
}
// Add the CombinedPayCode (which includes all the PayCodeNodes) to
// the object context and save the whole shebang.
_db.CombinedPayCodes.Add(combinedPayCode);
_db.SaveChanges();
I'm guess that you are using Entity Framework based off the syntax.
You can do this:
_db.CombinedPayCodes.Add(combinedPayCode);
_db.SaveChanges();
Then combinedPayCode.Id should have it's primary key. This not the best way to do it if you need that ID as a Foreign key on another table. Then you would do:
combinedPayCode.CombinedPayCodeLists.Add(combinedPayCodeList);
_db.CombinedPayCodes.Add(combinedPayCode);
_db.SaveChanges();
In this case you only have to save one time, and it wraps all the db changes in a transaction. If one fails, they all fail.
Hope this helps!
I'm having trouble performing an update in the Entity Framework. I don't really understand the behaviour I am seeing.
I am using the AdventureWorks database.
The starting value for the StateProvince is Micronesia. If I change it to Maryland, the update is successful. However, if I then try to change it back to Micronesia, I get the following error:
"Cannot insert duplicate key row in object 'Sales.SalesTerritory' with
unique index 'AK_SalesTerritory_Name'.\r\nThe statement has been
terminated."
The DAL method in question is:
public static void UpdateCustomer(CustomerDetails customerDetails)
{
AWEntities context = Common.GetContext();
var customerQuery = from c in context.Individuals
.Include("Contact")
.Include("Customer.CustomerAddresses.Address.StateProvince.SalesTerritory")
//.Include("Customer.SalesTerritory.StateProvinces")
.Where(id => id.CustomerID == customerDetails.CustomerId)
select c;
var individual = customerQuery.ToList().ElementAt(0);
Contact contact = individual.Contact;
contact.LastName = customerDetails.LastName;
contact.MiddleName = customerDetails.MiddleName;
contact.FirstName = customerDetails.FirstName;
contact.EmailAddress = customerDetails.EmailAddress;
contact.Phone = customerDetails.Phone;
contact.Title = customerDetails.Title;
AWModel.Customer customer = individual.Customer;
customer.CustomerID = customerDetails.CustomerId;
customer.SalesTerritory.Name = customerDetails.SalesTerritory;
Address address = individual.Customer.CustomerAddresses.ElementAt(0).Address;
address.AddressLine1 = customerDetails.AddressLine1;
address.AddressLine2 = customerDetails.AddressLine2;
address.City = customerDetails.City;
address.PostalCode = customerDetails.PostalCode;
address.StateProvinceID = customerDetails.SalesProvinceId;
context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
Can anyone identify the correct way to do what I am attempting.
This is occurring when you update the SalesTerritory.Name property:
customer.SalesTerritory.Name = customerDetails.SalesTerritory;
The effect is to change the SalesTerritory entity, rather than the customer entity. I believe you want something more like:
customer.SalesTerritoryID = customerDetails.SalesTerritoryID;