English 中文(简体)
Map a column to be IDENTITY in db
原标题:

Although I have marked my ID column with .Identity(), the generated database schema doesn t have IDENTITY set to true, which gives me problems when I m adding records. If I manually edit the database schema (in SQL Management Studio) to have the Id column marked IDENTITY, everything works as I want it - I just can t make EF do that by itself.

This is my complete mapping:

public class EntryConfiguration : EntityConfiguration<Entry>
{
    public EntryConfiguration()
    {
        Property(e => e.Id).IsIdentity();
        Property(e => e.Amount);
        Property(e => e.Description).IsRequired();
        Property(e => e.TransactionDate);

        Relationship(e => (ICollection<Tag>)e.Tags).FromProperty(t => t.Entries);
    }
}

As I m using EF to build and re-build the database for integration testing, I really need this to be done automatically...

EDIT: Hm... In a comment I was requested to give enough code to execute this, so I cut-and-pasted my code into a console app (so you wouldn t need all my classes...) and suddenly it just worked. I guess I was forgetting some method call somewhere, although I haven t been able to figure out where.

I ll post the working solution code in an answer to this post, in case someone else comes looking for it.

最佳回答

Running this code solves the problem. I guess I must have forgot a step somewhere, so if you have the same problem, make sure you do all these things:

var connection = GetUnOpenedSqlConnection();       // Any connection that inherits
                                                   // from DbConnection is fine.

var builder = new ContextBuilder<ObjectContext>(); // I actually had my own class
                                                   // that inherits from 
                                                   // ObjectContext, but that was 
                                                   // not the issue (I checked).

builder.Configurations.Add(EntryConfiguration);    // EntryConfiguration is the 
                                                   // class in the question

var context = builder.Create(connection);

if (context.DatabaseExists())
{ context.DeleteDatabase(); }

context.CreateDatabase();
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签