问题
The only thing I could find about enum support in breeze.js is this feature suggestion on uservoice, which was recently marked as closed in 0.82. I'm using currently latest, 0.84.3.
UPDATE: I'm using code first in EF 5.0.0 with .net 4.5. When starting application and breeze makes request for metadata, EF creates empty database and my enum property is in database as int, so that part is ok.
However, when I added an enum property to my model, I got exception when breeze tried to parse metadata:
Uncaught Error: Unable to locate an 'Type' by the name: ItemType:#TestApp.Models breeze.debug.js:5051
getTypeFromMap breeze.debug.js:5051
ctor.getEntityType breeze.debug.js:5028
ctor._updateProperty breeze.debug.js:6056
ctor._fixup breeze.debug.js:6133
ctor._fixup breeze.debug.js:6132
ctor.addEntityType breeze.debug.js:4702
convertFromODataEntityType
This is my model (simplified):
public enum ItemType
{
Ordered,
Approved,
Misc
}
public class Item
{
public long Id { get; set; }
public ItemType Type { get; set; }
}
Where am I making mistake? Is there working sample with enum?
回答1:
I just tried adding your ItemType enum to one of our models ( the ToDo model in the breeze DocCode sample) without a problem.
I am not sure what you are running into. So two suggestions,
1) Try updating (hack) the DocCode sample that ships within the breeze samples zip to use your ItemType enum ( details below) and then run any of the basic ToDo tests.
// In DocCode/Models/ToDoItem.cs
namespace Todo.Models
{
public class TodoItem
{
public int Id { get; set; } // 42
[Required, StringLength(maximumLength: 30)] // Validation rules
public string Description { get; set; } // "Get milk"
public System.DateTime CreatedAt { get; set; } // 25 August 2012, 9am PST
public bool IsDone { get; set; } // false
public bool IsArchived { get; set; } // false
// YOUR ENUM PROPERTY
public ItemType Type { get; set; }
}
// YOUR ENUM TYPE
public enum ItemType {
Ordered,
Approved,
Misc
}
}
// In DocCode/Models/ToDoDatabaseInitializer
private static TodoItem CreateTodo(string description, bool isDone, bool isArchived)
{
_baseCreatedAtDate = _baseCreatedAtDate.AddMinutes(1);
return new TodoItem
{
CreatedAt = _baseCreatedAtDate,
Description = description,
IsDone = isDone,
IsArchived = isArchived,
// YOUR ENUM PROPERTY
Type = ItemType.Ordered
};
}
or
2) Send me (Jay Traband) a stripped down version of your project at breeze@ideablade.com.
来源:https://stackoverflow.com/questions/14308529/exception-in-client-breeze-js-when-using-enum-property-on-model