FluentNHibernate and Enums

后端 未结 2 546
梦谈多话
梦谈多话 2021-02-11 01:01

I have an enum called Permissions. A user can be assigned permissions, or permissions can be asigned to a role and the user can be given a role.

User and Role both have

2条回答
  •  情深已故
    2021-02-11 01:27

    Just to make sure I understand, are you trying to just map an enum property in your class to a field in a database? If that's it, then here's an example of how to do that:

    public class Toy {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual ToyCondition Condition { get; set; }
    }
    
    public enum ToyCondition {
        New,
        Used
    }
    
    public class ToyMap : ClassMap {
        public ToyMap() {
            Id(x => x.Id);
            Map(x => x.Name);
            Map(x => x.Condition).CustomTypeIs(typeof(ToyCondition));
        }
    }
    

    After that you can get, set and do logic with the Condition property just like normal using the ToyCondition enum.

    Toy newToy = new Toy();
    newToy.Condition = ToyCondition.New;
    toyRepository.Save(newToy);
    

    In the database for the Condition field, it should be an int. I hope this made sense and answers your question. Sorry if it doesn't and I'm way off.

    Edit: Sorry I just noticed you asked this question and some one gave you the same answer as me. I don't think I can help you this. :(

提交回复
热议问题