Insert error with dapper dot net Cannot update identity column - sql-server

I'm new add dapper dot net so do forgive me if this question is quite easy for you.
I have my object wich I would like to save to te database. But when I execute my update command I get an error saying that I cannot update the identy column.
I'm not aloud to make any changes to the properties of my sql database, I'm using dapper extension and my project is written in vb.net.
How can I save my object?

A little old post but I was experiencing same problem. In my case I solved the problem creating a POCO class without the Identity column .
My entity is:
public class MyEntity
{
public decimal Id {get;set;} // Marked at SQL Db as Identity
other properties
}
To work with dapper-extensions Insert just remove Identity column at your POCO class
public class MyEntity
{
other properties
}
As you said you can't change the database, but at your code you could create your own poco's.

Related

Entity Framework data reader incompatible with column names with spaces (database first)

I'm trying to wrap Entity Framework (6.4.0) around a SQL View (SQL Server) where a couple of columns have spaces in the column names. For example, one column in SQL is "Badge ID", when I wrap EF around this view it renames the column "Badge_ID"
When I try to query, EF throws exception that the data reader is incompatible (makes sense):
System.Data.Entity.Core.EntityCommandExecutionException: The data
reader is incompatible with the specified 'Model.vwEmpOrg'. A member
of the type, 'Badge_ID' does not have a corresponding column in the
data reader with the same name.'
I tried a solution from a code-first approach where in the class definition you can annotate the column name like so:
namespace EXAMPLE.Models
{
using System.ComponentModel.DataAnnotations.Schema;
public partial class vwEmpOrg
{
[Column("Badge ID")]
public string Badge_ID { get; set; }
However the same is exception is still thrown. What am I missing? Why doesn't the annotation on the column name work?
You will need to post more code specific to your Entity, especially anything that has been auto-generated, and the underlying View because EF6 + SQL Server have no issues with Spaces in column names using DB First.
AFAIK you cannot leverage Code-First with SQL Views. Code First would expect to create a Table called vwEmpOrg. You can certainly map an EF Entity to an existing view using annotations or explicit entity configurations.
For example I have a Table called Persons with PersonId & Name, I create a View called vwPersons with:
SELECT PersonId AS [Person ID], Name
FROM dbo.Persons
Then for an entity:
[Table("vwPersons")]
public class ViewPersons
{
[Key, Column("Person ID")]
public int PersonId { get; set; }
public string Name { get; set; }
}
This works perfectly fine. For argument's sake to ensure that EF wasn't pulling any defaulting to conventions, I renamed the view's column alias to "Some ID", and renamed the key in the Entity to "Fudgesicle" pointed at "Some ID", and it was fine pulling that. (Nothing linking back to a "PersonId" in the underlying table)
I'd check that your code is actually using that entity definition for vwEmpOrg or the mapped BadgeId and not that the DbContext is actually using a generated model class in a different namespace. (If you had been using EDMX's or similar)

Entity framework foregin key in another database

So i like MVC and EF6 but I keep coming across fundamental problems with the way it / I work.
I have an app ( a very simple one) in there one of my tables references a field in another database, how would EF handle this , it seems to get very complicated whereas in the past it would have been a simple ADO.NET call to a stored procedure or something ( I am aware I can use SP's with EF, but really, what's the point , may as well just use ADO.NET again), example model below:
[Table("Target")]
public partial class Target
{
public int ID { get; set; }
public int SomeForeignKeyInMyDbID { get; set; }
public Guid? FOREGINKEYINANOTHERDB { get; set; }
}
when I scaffold views based of this it automatically creates the drop down menus etc really well but it (obviously) cannot pickup the reference to the foreign key in another field, as I want to store the ID of the foreign key in the database but get the value of it for drop downs etc, I store the ID instead of the value for reporting reasons.
I thought that I would just be able to get a context to my other db, grab the values I need and bind them to the drop down list but the model structure is so tightly defined that I face hurdle after hurdle on this.
I read somewhere that my best option may be to use SP's for CRUD operations and then perform a LINQ to EF query fro the index view and do a join on foreginkeyfromanotherdb field.
Any help much appreciated.
Thanks

EntityFramework code-first ignoring derived columns when creating database

I would like some of tables in my database to have standard columns such as createdBy, ModifiedBy, CreatedDateTime, modifiedDateTime etc.
So, I created an interface with those properties and implemented the interface in an abstract base class. I derived my concrete classes from this base class.
This is a brand new application using Code-First approach. When I create the database, the derived properties are ignored. The tables are created with just the properties in the derived classes.
I'm not sure why.
Thank you.
I'm not sure exactly what was different, but tried it again with the following changes and it worked:
gave dbCreator rights in the database for the user
Changed the properties in my base class to be in the format (similar change for the other properties):
public string CreatedBy { get; set;}

Many-to-Many relationship in Zend 2 Framework

I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/
The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.
I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.
What I have tried so far is the following, that's code from my VotingTable class:
public function getTagsByVoting($votingHash){
$select = $this->tableGateway->getSql()->select();
$select->from(array('v' => 'voting'))
->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id')
->join('tags', 'voting_tag_map.tag_id=tags.tag_id');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
It says then:
Since this object was created with a table and/or schema in the constructor, it is read only.
Thats because of the from() method. If I delete the from() method, it says:
Statement could not be executed
Can anyone help me please?
Since this object was created with a table and/or schema in the constructor, it is read only.
This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.
If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...
The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'

Entity Framework Code First: How to map SQL Server's computed columns into CF model?

Is it possible to map computed columns into Code First model?
I'd like to define computed columns with SQL Server and then map them as properties inside my CF model.
Thanks in advance
Best Regards
Use the DatabaseGenerated attribute with DatabaseGeneratedOption.Computed as the value
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Foo { get; set; }
Not sure how you would do computed columns without using a view, and you cannot use views in EF Code First (you can in Model & DB first though). EF will execute some operations in SQL for you, instead of in code, but it doesn't sound like that is what you're looking for. Can you elaborate on what you are trying to achieve?
I think for EFCF beginners (like me!), there is no need for creating Database Initializers, this just work:
Model1 model = new Model1();//Model1 : DbContext
model.Database.CreateIfNotExists();
model.Database.ExecuteSqlCommand("alter table Results drop column Total; alter table Results add Total AS (Arabic + English + Math + Science)");
for the complete answer check here.

Resources