Right now I have file upload/download through Entity Framework working but I see an issue coming up. In the scenario when i want to get a list of all the files associated with a record, I don't want it to pull the Data property, just the FileId and Name, because the files can be up to 10MB each.
I have LazyLoading disabled so I'm thinking about putting the Data column into another table and only load the data when I want. That way I can just supply a link to a controller with the FileId I want to download. But maybe there is a better way? All suggestions are appreciated. Thanks!
My File entity has the following properties:
FileId
FkRecord
Name
Data
you do not need to put data column in another table - just create another entity in the designer and move you [Data] column in it. don't forget to create corresponding table mapping in designer - map you data column to the column in db table.
Also create 1 to 1 association between entities. And you could use navigational properties and do not need to alter you db table!
I found similar discussion:
Can I lazy load scalar properties with the ADO.Net Entity Framework?
Related
I read data from one of more remote files into coredata. If a remote file is changed the data in core data needs to be updated. For instance, assume one file is filled with departments, the other with employees.
As the company is restructured, one department is renamed and a second department without employees is deleted in the file. The employees file is not changed, so I only want to reread the file with departments. In my code I read the file, fetch the department from coredata and updates its name property. But since the second department is no longer in the file, I want to delete it from core data.
My pseudocode solution is as follows:
the department entity gets a hasChanged attribute
before reading from the file, all hasChanged attributes are set to false
if a department is present in the file, its hasChanged attribute is set to true
after the file is read, all departments with hasChanged attribute set to false are fetched and deleted
Somehow the seems not very efficient. Deleting all departments and building them a new seems also not very efficient, because core data will delete all employees with the departments and now I have to reread the employees (and probably all other files) also.
Is there a better way to approach this problem of data becoming superfluous? If you answer with code, swift is preferable.
First the delete rule should not be cascade - change it to nullify.
When you get new data from the server follow these steps:
fetch all of the entities that are effected. If you are updating departments - then fetch all the departments
store the results in a dictionary where the ID is the key.
Also store all the results in a mutable set called objectsToDelete
now iterate through all the data that you got from the server. lookup the department using the dictionary you made in step #2. If you find the object, then update it and remove it from the set you created in step #3. If you don't find the object then create it.
If there are any objects left in objectsToDelete then delete them
save the context.
It is the same principle for the employees. You match up the ones you already have using a dictionary, and delete the ones that don't get matched up.
I have a table in that table i'm inserting all my TableNames of that database.
Now I need to pass this table name to OLEDB source DYNAMICALLY from my main table one by one
Is this possible to to pass the table name as dynamically in OLEDB source.
I suspect you are then going to run some SQL against the stored table name?
You'll need to approach this differently and run what you are trying within a SQL task.
If not, give us some more information about exactly what you are trying to achieve.
I have a static table for settings where I want to pull some stuff from an entity in Core Data. The use case does not lend itself to a table of records as you usually see. Rather each row of the static table is really a field related to the user--as in a user profile. I have a feeling that in testing I may have created more than one record in the entity. I know there are programs that let you see the SQL lite database underneath, but my question assumes you do not have this tool and are relying just on Xcode.
My question is when you have more than one record in a Core Data entity/table, and you try to load data from the managed object context into a VC, one field into one element, what record is shown by default?
Related to this, if you don't know how many managed object or rows are in the database, is there anyway to specify which record you want since there are no auto ids as you would use in a traditional database?
The record that gets loaded from the fetch first. Depending on your sort that might be consistent or it might be random.
I am developing MVC application and using razor syntax. I have used model first method.
I have two entities, Customer and Lead. Lead is inherited from Customer.
When IsActive property is true then Customer treated as a Lead, otherwise it will be a customer.
Please check edmx file image.
Now, In regular entities we just deal with single entity and single table.
In this case how can I handle , Save and Load process. beacuse I have to store and load the record from 2 tables of DB.
Is Regular Index View will work here ?
When using inheritance in the Entity Framework you will have a single DbSet on your DbContext that exposes your hierarchy. In your database you have several options for configuring your table structure. For example you can use:
Table per Hierarchy
Table per Type
Table per Concrete type
(See this blog for a nice explanation: Inheritance in the Entity Framework.
In your queries however, you don't have to think about this. Your queries will have the following structure:
var leads = from l in dbcontext.Leads.OfType<Customer>()
select l;
The OfType() filters your collection to a subtype in your hierarchy. If you skip the OfType you will get both customers and leads in your resulting query.
I have an entity (Org) in my Entity Framework that has a foreign key with a table that is in another database (BusinessUnit). We need the foreign key to get the description of the BusinessUnit linked with the Org. In the past (old project without Entity Framework) we were using a stored procedure to return all the information about this entity, including the BusinessUnit description, using a join. So now my problem is how to display the same information than before using Entity Framework.
I've tried, once I load my Org entity from database, to make a loop accessing to BusinessUnit to get the description for each Org, but this is too slow. My other idea was use a store procedure, but I need an extra field on my entity and Entity Frameworks gives me an 3004 error: No mapping specified for my property. I was thinking to use a complex type, but I'm not sure if it's what I need keeping in mind I have to add just a field to my entity. In that case, could I use the complex type just for "select" operations and keep my original entity for the rest of CRUD operations?
How should I proceed?
Thanks.
EF is not able to execute queries across multiple databases. If you need to perform such query you must either use database view and map the view as a new entity (it will be readonly - making it updatable requires mapping insert, update and delete stored procedures) or divide your data querying into two separate parts to load data from both databases. Divided querying can either use two contexts or you can get data from the second database using stored procedure.
The reason why you got the error is that you added the property in EDMX. EDMX can contain only properties mapped to your first database. EDMX generates entity code as partial classes If you need property manually populated from the second database you have to create your partial part (partial class) for entity and add the property in code.