I am using entity framework 6 for one of my project. It has two entities Employee and EmployeeContacts. One employee can have many contacts and I have mapped these relationship in the database. When I return an Employee object it also returns EmployeeContacts that is related to the Employee. I want get only the Employee object excluding contact details
You can use find: var myEmployee = context.Employee.Find(id);
See https://msdn.microsoft.com/en-us/data/jj573936.aspx
Related
I have an Entity that has four properties, (vehicle, unit, date and id). Primary key is the ID.
I want to delete rows from the database based on the vehicle list provided to me via a request body.
How can I take all the lists and use them to delete data from the database at once?
You can create a "delete...By" query in your Entity repository that takes a List of Vehicle as a parameter and deletes all entities that their Vehicle is contained in that List.
Something like this should work:
void deleteAllByVehicle(List<Vehicle> vehicles);
The documentation contains more options:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods
you can use JPQL to provide custom query
#Query(delete from Entity e where e.idvehicle = :id)
void deleteByVehicle(#Param("id") int idvehicle);
now you can just pass the id of the Vehicle like that:
deleteByVehicle(vehicle.getId());
I have 4 tables:
Accounts
Photos
AccountPhotos (AccuntID, PhotoID) - Holds records of bought photos
for each account
AccountFlagPhotos (AccountID, PhotoID) - Holds records of flagged
photos for each account
Thus I have 2 many to many relationships between the same 2 tables, holding the same data but for different purposes.
I usually SELECT records in LINQ between many to many relationship tables as follows:
public IQueryable<Photo> GetByAccount(string username)
{
//Get the specific Account record
Account myAccount = new UserDAL().GetByID(username);
//Get all photos for that account (many to many)
return myAccount.Photos.AsQueryable();
}
The problem is that this time I have two many to many relationships between the same two tables. How can I determine in the code above from which table (table 3 or 4) I want to retrieve records from?
Go to the Entity Model
Right click on the relationship
Select Properties
Use the End2 Navigation Property
N.B.:
From the diagram you can directly go to the relationship properties by selecting the relationship from the Navigation Properties of a particular table.
I have two tables, in which one user can have several contacts:
User(UserId, UserName)
Contact(UserId, ContactId)
Suppose I would like to get all the ContactNames from the UserNames in the User Table by the Userid.
Note that the Contact table cannot be seen in the current data context, it has been turned into a many to many relationship
How can I do the query?
What If I need to insert or delete?
I tried the "include" method but it does not work. Do you have any suggestions?
Thank you very much.
var id = 1; // id to find
context.Users
.Where(x=>x.UserId = id)
.SelectMany(x=>x.Users)
.Select(x=>x.UserName)
.ToArray();
After generation from db your model has 2 sub-collections: Users and Users1.
One of it corresponds to users, that are contacts for current user.
Another stores users, that current user is contact for.
You can rename them to represent their meaning to Contacts and ContactsFor via editor.
If you still want to have 2 types instead of Type+(Many-To-Many Ref), then in editor you can delete reference, create new entity Contact, setup all mapping, add references. After all this is done - you will have model look like this:
To Achieve this:
Delete Many-To-Many reference
Crete new Entity Contact
Add properties ContactId and UserId, set StoreGeneratedPattern to none
Add mappings for Contact
Add associations for Contacts and ContactFor
But it's not that easy.
Suppose I have an automatically-generated Employee class based on the Employees table in my database.
Now suppose that I want to pass employee data to a ShowAges method that will print out name & age for a list of employees. I'll retrieve the data for a given set of employees via a linq query, which will return me a set of Employee instances. I can then pass the Employee instances to the ShowAges method, which can access the Name & Age fields to get the data it needs.
However, because my Employees table has relationships with various other tables in my database, my Employee class also has a Department field, a Manager field, etc. that provide access to related records in those other tables. If the ShowAges method were to invoke any of those methods, this would cause lots more data to be fetched from the database, on-demand.
I want to be sure that the ShowAges method only uses the data I have already fetched for it, but I really don't want to have to go to the trouble of defining a new class which replicates the Employee class but has fewer methods. (In my real-world scenario, the class would have to be considerably more complex than the Employee class described here; it would have several 'joined' classes that do need to be populated, and others that don't).
Is there a way to 'switch off' or 'disconnect' the Employees instances so that an attempt to access any property or related object that's not already populated will raise an exception?
If not, then I assume that since this must be a common requirement, there might be an already-established pattern for doing this sort of thing?
maybe not the answer you're looking for,but how about projecting the results of your query into a more light-weight POCO, eg:
var employeePOCOs = from e in l2sEmployees
select new EmployeePOCO
{
Id = e.Id,
Name = e.FirstName + " " + e.LastName
};
where EmployeePOCO is a predefined class
would that help? I've used this when returning Entity Framework objects back through an AJAX call where the output was going to JSON, and it seemed to do the trick.
One way to do this is to 'detach' the entity from its database context. Take a look at an answer I gave to a similar question. It shows you a couple ways of detaching entities.
I have an entity (e.g. Employee) in a managed object model that is related to two other entities (e.g. Department and Team). Both relationships are one-to-many (i.e. an Employee must have one Department and one Team, Teams and Departments have many Employees). The two may or may not overlap (e.g. a team might be made up of employees from HR, Accounting & I.T. or it might jut consist of several employees from the one department).
Department <-->> Employee <<--> Team
I have two NSArrayControllers providing data for two NSTableViews, a Department table and a Team table. Employees can move between departments and between teams without any problems but I'm not sure how to delete (fire) the employee.
If I send either of the array controllers a remove message the employee is taken out of the team (for example) but left in the department and the object graph is in an inconsistent state. Even if I call the remove action on both controllers the object is not deleted - it is orphaned and just hangs around in limbo.
Originally I had the Department & Team relationships (of the Employee entity) set to a delete rule of Nullify but even changing one or both to cascade doesn't help.
Do I need to override the remove: action on the array controllers to actually delete the employee or am I missing something really obvious?
The NSArrayController has two different behaviors when you're using Core Data. If it is configured to simply fetch objects directly from the managed object context, it will delete the objects when they are removed.
If you're binding the contentSet to another controller, like it sounds like you are in this case, the default behavior is to simply remove the object from the relationship. If you want to delete it, though, there is a "deletes object on remove" binding option, which will produce the result you want.