7

Creating EF Controls in MVC with Enums as Keys

 3 years ago
source link: https://www.codesd.com/item/creating-ef-controls-in-mvc-with-enums-as-keys.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Creating EF Controls in MVC with Enums as Keys

advertisements

Ok so here's some context: I'm using EF5, MVC4 and SQL CE4 to build up a web application. I've been loosely following this tutorial with a few differences.

  1. My context class and POCO objects are in their own assembly.
  2. I'm using SQL CE4 instead of SQL Express Local DB
  3. My classes aren't as simple as the tutorial

I've already used a workaround to get simple classes to work register.

I had thought using enums in EF5 was supported in EF5, but can they be used in Keys?

When I try to add a control (Add Controller, MVC controller with read/write actions and views, using Entity Framework) for a simple class (1 int key property, 1 string property), it works. I get varied errors when trying to add a class that has a property which is part of a key (primary or foreign)

Unable to retrieve metadata for 'blah'.  Using the
same DbCompiledModel to create contexts against different types of
database servers is not supported. Instead, create a separate
DbCompiledModel for each type of server being used.

Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

C:\Program Files (x86)\Microsoft Visual Studio\11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\
CodeTemplates\AddController\ControllerWithContext.tt(0,0) : error :
Running transformation: System.IndexOutOfRangeException: Index was outside the bounds of the
array.
---StackTrace---

The only similarities I've found between these classes is that they have an emun that's tied to a key. Other classes with non-key enums generate correctly. Is this the issue or have I completely missed the mark?

Edit: Example of a class which fails

public class A
{
    public virtual AIdEnum Id { get; set; }
    public virtual string Name { get; set; }

    public virtual ICollection<B> Bs { get; set; }
    public virtual ICollection<C> Cs { get; set; }
}


Ok, so I've just ran this up quickly with SQL CE 4 and it appears to work great:

public class EnumTestContext : DbContext
{
    public EnumTestContext() : base("CEDB")
    {

    }

    public DbSet<MyClass> MyClasses { get; set; }

}

public enum MyEnum
{
    EnumValue1,
    EnumValue2
}

public class MyClass
{
    [Key, Column(Order = 0)]
    public MyEnum MyEnumKey { get; set; }

    [Key, Column(Order = 1)]
    public int MyIntKey { get; set; }

    [Column(Order = 2)]
    public string Name { get; set; }

}

I then add an entity like this:

using (var context = new EnumTestContext())
{
    context.MyClasses.Add(new MyClass()
        {
        MyEnumKey = MyEnum.EnumValue1,
        MyIntKey = 22,
        Name = "Hello World"
        });
    context.SaveChanges();
}

This is all working fine for me - does this help?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK