-1

I have a question. I don't really know if it's possible. I'm working on an MVC application and part of the model look like this:

public class EmployeeNewspaper
{
    public string Name { get; set; }
    [Key]
    public string Appendage { get; set; }
    public string SourceInformationName { get; set; }
}

So my question is: Is it possible to have two keys in the same class? Or what can I do? I need to have the name and the appendage unique. Is it possible?

ekad
  • 14,436
  • 26
  • 44
  • 46
SomeAnonymousPerson
  • 3,173
  • 1
  • 21
  • 22
  • 4
    It's not entirely clear what the question is - is this an entity framework model or a standard MVC model? If it's a standard MVC model, do you want your unique-ness to be enforced by your code or in your user interface? (e.g. do you want a runtime error or a validation error if a duplicate is added... to something?) - if you can clarify where you want to enforce uniqueness it would be useful as the approach will differ (DB keys, hashes/equality comparer, validation rules etc) – Charleh Jul 26 '16 at 13:40
  • Its an entity framework model. I want to show to the user that unique information – SomeAnonymousPerson Jul 26 '16 at 13:46
  • When I run the stored procedure in the DB I have the correct information. But when I pass the information to the model, as I only have the key in the Appendage, it it has different names, its showing me the same name in any case. So I need to make the name and the appendage key (together) both of us in any case. Did I explain myself? – SomeAnonymousPerson Jul 26 '16 at 13:48
  • 1
    You want a composite key then: http://stackoverflow.com/questions/14873169/creating-composite-key-entity-framework – Charleh Jul 26 '16 at 13:56

2 Answers2

1

Thanks @Charle I was actually looking for composite key and the answer is:

public class EmployeeNewspaper
{
    [Key][Column(Order = 0)]
    public string Name { get; set; }
    [Key][Column(Order = 1)]
    public string Appendage { get; set; }
    public string SourceInformationName { get; set; }
}
Community
  • 1
  • 1
SomeAnonymousPerson
  • 3,173
  • 1
  • 21
  • 22
0

You could use a Guid and get a Globally Unique IDentifier.

But you will need to be aware that using this:

Guid MyID = new Guid();

will result in an all-zeros(empty) ID.

So you will need to use it this way:

Guid MyID = Guid.NewGuid();
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104