Adding unexisting entities while creating another entity - asp.net

I'm setting up a blog using Asp.net MVC3 and Entity framework 4. Most of the properties of the blog post already exist in the database, and therefore can easily be linked to the blog post during the creation.
One of the last element I've added are sources, these sources have a direct link to my BlogPost and have to be added during the creation of the BlogPost. My idea was to use the entity property Sources of the BlogPost entity, like:
blogPost.Sources.Add(new Source() { Name = source.Value, Url = source.Key.ToString(), Type = "source" });
Unfortunately I'm not allowed to create and add an Source this way, I'm getting the error: "Cannot insert explicit value for identity column in table 'Source' when IDENTITY_INSERT is set to OFF."
I guess this is cause of the ID column which has is an Identity with Identity increment turned on, but I don't set any ID myself. I tried to make id = null, but since its not a null-able it's not allowed either.
I believe turning this feature off let's me add records with my own ID's, but this is not what I want. The database should create the ID's.
Is there a way to add these kinds of properties during creation?

I solved the problem by adding the Source entity again from the database, apparently the entity framework model was generated before the field was set to Identity Increasing and therefore wanted to insert it's own ID's.

Related

How to set a deleted property to true rather than removing a related doctrine entity in Symfony

I'm building an app that allows a user to create reports for advertisers. The entities are set up so that there is a relation between the Report object and the Advertiser object - so that the advertiser has a getReports() method to get them.
I would like to change the app so that instead of actually deleting entities, that it simply changes a "deleted" property to true. That part is no problem, but I'm unsure how to make it so that the getReports() on the Advertiser entity only returns reports for the advertiser that have a deleted property of false.
Please let me know if you have any suggestions how that should be done in accordance with Symfony best practices.
You should look into Gedmo Doctrine Extensions. http://atlantic18.github.io/DoctrineExtensions/
Specifically for your case:
http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html
TLDR; they allow you to configure behavior of your entities in a way you desire, so for example when you "delete" an entity, Gedmo's listeners would set it's deleted value to a current datetime. Now you'd still have that record in your database but with not null value of deleted column marking it 'soft deleted', so when querying, it wouldn't be returned (because Doctrine knows how to query these stuff and would add a condition i.e.: ... where deleted ...) unless you explicitly say you want to see those soft deleted records.

A few questions regarding importing a manually created data entity

I used the data entity creation wizard and selected Reqplan table as the main data source, then I manually added ReqPlanVersion, ReqPO, ReqTrans table as additional data sources and created the relationships below.
As for the data entities fields I manually dragged a subsets of fields from the three manually added tables.
However when I try to import the data and add file, I receive the following issue:
Q1. In the past for some other entities I have changed ‘Allow Edit on Create’ from ‘Auto’ to ‘YES’ on these fields and it has worked but I am not sure if it’s the only way or is it following best practice? Also what is the determining factor for a field to be editable or not during import since they are all on AUTO?
When I try to map source to staging manually by drawing the mapping lines I get below issue:
Q2. What is going on with the configuration key? Is it because I manually dragged the fields from the additional data sources but not using the data entity creation wizard?
Lastly I been getting below issue:
Q3: Is there a way to find out which unique key it is referring to? Is it talking about the EntityKey in my Data Entity or Indexes in staging table? In either case there are more than one so I am not sure what it is referring to?
Thanks in advance.
Response from the community forum:
1) Check allowEdit property on table itself, so if it is "No" there then auto means "No". If you want to update them through data entity you will have to force them to "Yes"
2) It's not connected to manual addition, it just says that tables used in the entity has configuration key disabled, so you cannot export or import data into them, however, these tables could be added by wizard or manually, it does not matter. Also, Configuration key could be on fields as well or on EDT that these fields use, check them as well.
3) Entity has Key node and there and there you have key generated by wizard for you. It is used by framework to understand if record should be updated or created, if it does not work for you, change it on the data entity and regenerate staging. You need to refresh staging because error you get is SQL error, at this stage SSIS transfers data from a file into a staging table and data could not be copied because of index vialotion, so check staging table index and see if your file has any duplicates.

How do we give default value to Primary Key fields in websites created through ASP.NET Dynamic Data (Entities)

I am creating ASP.NET Dynamic Data Site (using entities) to manage master tables of my application.
All my master table has a Primary Key of "UniqueIdentifier" in SQL Server. Where should I write "Guid.NewGuid()" in order to give default value (as new Guid) and to show them in textboxes bound with these fields?
ok, I found the solution myself.
We need to open EDMX file, and ID property in each entity needs to be have StoreGeneratedPattern="Computed"

umbraco and custom system table

I have the following scenario:
My website db has a system table called "Companies", which includes an id field, companyName field, and companyImageUrl field.
How do I set up an umbraco document type for adding entries to this table ?
Maybe I shouldn't use a custom table at all ?
Thanks.
As far as I know, Umbraco doesn't support what you want to do out of the box (mapping a document type to a table that isn't part of the umbraco core).
One approach that might work is to create an action handler that syncs a Company doc type to your table when creating a node of that type.
It's a bit of a hack though. I've found that I've very rarely needed to create custom tables. What exactly are you trying to do with it? My guess is that you don't really need it and would be better off working with a doc type instead. Umbraco provides a variety of ways to get and act upon doc types from within custom C# code (check out the umbraco.NodeFactory namespace). You'll also get the added benefit of being able to easily interact with these nodes from XSLT/Razor.

Exclude column from being updateable in Entity Framework 4.1 Code First

Does anyone know if we can exclude column from being updated in Entity Framework 4.1 Code First? For example I have 'CreatedOn' field that I don't want to included when doing edit/updates. Is this possible, i.e. selectively excluding field from update operation in EF Code First 4.1?
If you are working with attached entities you EF will generate updates only for fields which have changed. If you are working with detached entities you must manually say EF what has changed. If you call this:
context.Entry(yourEntity).State = EntityState.Modified;
you are saying EF that all properties should by modified. But if you instead call this:
context.Entry(youreEntity).Property(e => e.SomeProperty).IsModified = true;
you will say that only SomeProperty is modified (only this property will be in update). I'm not sure if you can do the reverse operation by marking the whole entity as modified and select properties which should not be modified but you can test it yourselves.
If your CreatedOn is filled in the database you can mark it as DatabaseGeneratedOption.Identity and it will be never modified by your application.

Resources