Apigee entity primary key - apigee

I have a custom entity Entity1, and I need to make it id field primary key. How to do it?
I expect that when I call mDataClient.createEntityAsync - it will update the entity if the same id already exists, currently it creates a new one.
Thanks.

Right now we do not support a primary key other than 'name' which is the default on all collections. Can you use 'name' instead of 'id'?

Related

Symfony2: referring foreign keys in symfony2

This is my query to get the desired product and set its values in symfony2
$item->setProduct($id)
->setQuantity(1)
->setTemp($s1);
While using foreign keys you should use
(getReference('YourBundle:EntityName',$id)

Entity Model cannot accept View without id?

SELECT DISTINCT TOP (100)
PERCENT country_Code, country_Name
FROM dbo.Location
Message 1 The table/view 'mixtapez.dbo.View_Select_Country' does not
have a primary key defined. The key has been inferred and the
definition was created as a read-only
table/view. E:\1C#asp.net\vuziq\vuziq\Projects\BannerSystem\WebBannerSystem\WebBannerSystem\Models\Model1.edmx 0 0 WebBannerSystem
The view works on others languages, so I dont want get Id by distinct, any idea?
It is just an informative message that Entity Framework created what it thinks is the primary key because any Entity Framework entity must have a primary key (that is .NET key, not SQL key). If you have read-only entity, the autogenerated key will probably work just fine for you. To be extra safe (and remove any chance the key does not work correctly), you should use NoTracking option for queries on this entity.
If you use .Distinct() in LINQ query that will go into your SQL query - the key Entity Framework uses does not play any role.

asp.net Entity Framework/ Update from database/ The table/view does not have a primary key defined and no valid primary key could be inferred

One of the database view I am trying to import using entity framework contains only two columns, one is an integer type of column and another one is an aggregate function. I am getting the following error.
The table/view does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.
I understand it is a known scenario and it can be fixed by either including a Key column in the view or modifying the edmx file manually.
I just wanted to know if there is some other solution other than the above two? I do not want to include an additional column in my query and making changes in edmx is not feasible as DB changes are very frequent and the edmx will be overwritten every time I update from db.
You can mark both properties as entity key directly in the designer but you must ensure that the composite value of these two properties will be always unique. If you cannot ensure that you must add another unique column anyway or you may have some other problems when working with such entity set.

Does an Entity Framework Foreign Key Association Always Imply Cascade Delete?

After reading Foreign Keys in the Entity Framework I tried adding a Foreign Key relationship to an EF 5 Code First application (the app uses Independent Associations to date).
I noticed that the generated DDL includes a cascade delete. This is the opposite of the behavior with Independent Associations.
Given that a Foreign Key relationship is created in EF Code First by adding an int property named according to the convention ClassnameId, is it possible to have a non-cascading delete? If so, what value would be assigned to ClassnameId to disassociate the related object without deleting it from the database?
If you explicitly declare a foreign key, that is non nullable, EF will assume that you want a cascading delete.
You can either make the Foreign key nullable:
public int? YourFkId {get;set;}
Or you can use fluent notations in your OnModelCreating
modelBuilder.Entity<Class1>()
.HasMany( c => c.Class2s )
.WithRequired(x => x.Class1 )
.WillCascadeOnDelete(false);

How to update a aprimary key field with linq to SQL?

I have tbl_names with following fields:
Id int
Name nvarchar(10)
family nvarchar(20)
Id Name Family
1 John Smith
and suppose Id and name are primary key together(compound primary key).
and I want to update name field according to the value of Id field.
DataclassesContext dac=new DataClassesContext();
var query=from record in Dac.tbl_name where record.id=1 select record;
query.name="Raymond";
Dac.Submitchanges();
but I encounter following error:
Value of member 'co_Workshop' of an object of type 'Tbl_Workshop' changed.
A member defining the identity of the object cannot be changed.
Consider adding a new object with new identity and deleting the existing one instead.
Is it because of name field is primary key? why can't I update a primary key field using linq?
I am not sure that you should find a way around this. I cannot imagine why it would be a good idea to change a value in a PK. The entire nature of a PK is that it is a stable identifier of the row.
In your case, you should drop and recreate the PK to be just the "Id" field and then if you need to improve performance on queries filtering on "name" then just add an Index on the "name" field. The fact that you only use the "Id" field to find the record supports this idea.
EDIT:
I answered before there were comments to the Question. Now that I see the comment from the OP about "it is an old database and can't change it's structure", I would say that if there are no FKs pointing to this PK then this should be a fairly straight-forward change (to drop and recreate the PK with just the "Id" field as I mentioned above). If there are FKs pointing to it then an option (though not a great option and it might not work on all RDBMS's) is to:
Drop the FKs
Drop the PK
Create the new PK on just the "Id" field
Create a UNIQUE INDEX on "Id" and "Name"
Recreate the FK's to point to the UNIQUE INDEX
This will work on Microsoft SQL Server and as much as I dislike the idea of a FK pointing to a UNIQUE INDEX, it should allow for the same structure that you have now plus LINQ will just see the single field PK on "Id" and allow for the update.
Where possible, a workaround is to delete the record whose primary key value needs updating and create a new record in its place.
It looks like there are ways around it, like I mentioned above. Linq won't let you change the primary key.
http://social.msdn.microsoft.com/Forums/en/linqprojectgeneral/thread/64064c2d-1484-4a00-a2c4-764bcb6b774a
Had the same problem. For legacy reasons, I couldn't remove the column I needed to update from being part of the primary key.
A simple solution was to not use Linq in this case, but use T_SQL and do a simple update.
update tbl_name set name = 'Raymond' where id = 1

Resources