0

I'm working on a project that uses MvcScaffolding to build a quick CRUD interface (many thanks to Steven Sanderson), but I'm having trouble adding two 'code-first' fields (I normally work in SQL Server and know very little about C#).

I'd like to add a column for the SQL equivalent of DEFAULT (getdate()), and a varchar field for representing the name of the user logged in on via Forms Authentication on the project (I believe this is storing HttpContext.Current.User.Identity.Name, but I'm not certain).

The end goal is to allow users to log in; create a new record & save; and then the list/index page only returns records where their username is stored in the CreatedBy field (in SQL, something like WHERE CreatedBy = <HttpContext.Current.User.Identity.Name>).

Any suggestions appreciated!

4

1 回答 1

1

I feel your pain. I remember encountering this problem myself.

The sad and simple answer is that there is no default annotation that you can use with Code First yet. There is still a lot to be desired when it comes to Code First.

Below is a list of all available annotations that the Entity Framework offers. As you can see it is still a bit limited.

http://msdn.microsoft.com/en-us/library/gg197525(v=VS.103).aspx

When using the Entity Framework, I myself rely on using a simple EDMX file generated from a database I have created from scratch. This allows you to not have to worry about writing all the CRUD statements and still provides control over the little things.

As far as MVC Scaffolding goes, the EDMX file works exactly the same as a Code First model. I am a huge fan of MVC Scaffolding and I use it with an EDMX file for multiple projects.

In regards to your goal of returning records where their username is stored in the CreatedBy field. There are many way to solve this issue. You can create a query with LINQ or you can write a stored procedure yourself and import it with the EDMX file (another strong point to using EDMX over Code First).

With LINQ it would look something like this:

    public List<Record> GetAllRecordsByUserName(string credentials)
    {
        List<Record> recordList;
        using (CustomEntities context = new CustomEntities())
        {

            IQueryable<Section> recordQuery = from records in context.Records
                                                  where records.UserName == credentials
                                                  select records; 
            recordList = recordQuery.ToList<Record>();
        }
        return recordList;
    }


As for writing a stored procedure, you would simply write your stored procedure in TSQL. When the EDMX file has been generated you would right click on the screen and select "Import Function". From there you would find your stored procedure and you will have the option of automatically mapping it to the object you desire, or creating a new Complex Type if the object type does not exist. This is a very powerful tool and it performs better than using the LINQ statement written above.

于 2012-05-03T19:03:23.810 回答