0

I need to create a complex value via Entity Framework code-first, which includes three the following three values.

  • A is a constant part look like "s#34"
  • B is float and it's value come from a API webservice
  • C is natural number!

I want to C generate automatically . In one example we have these Ids :

+-----+------+--+
|A    |B     |C |
+-----+------+--+
|s#34 |67889 |1 |
|s#34 |68988 |2 |
|s#34 |87665 |3 |
|s#34 |35458 |4 |
|s#34 |..... |5 |
...

I'm looking for a code like this:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[ID = "string1" + "string2" + default.ToString()]
public int ID { get; set; }

my mean from "default" in code come back to the value that get from (DatabaseGeneratedOption.Identity).

Thank you for your help

  • Mind showing us what have you tried? Where are you having problem specifically? The `C` value you can generate either via `identity` or `sequence`, `A` you can hardcode in your code meanwhile `B` is just a normal variable you fetch from your webservice. – Rosdi Kasim Sep 17 '16 at 05:55
  • my question is about automatic generator! I don't want to check last entity ID in db to create a new row I want to use DataAnnotations abilities look like:"[DatabaseGenerated(DatabaseGeneratedOption)" – Muhammad Ehsan Mirzaei Sep 18 '16 at 17:51
  • You could achive this using SQL Server custom identity column... refer here: http://stackoverflow.com/questions/2177584/sqlserver-identity-column-with-text – Rosdi Kasim Sep 19 '16 at 03:28

1 Answers1

0

Cramming all three pieces of information into one field would violate the single responsibility principle. You should probably create a composite key, and then combine in a computed column if you need to concatenate the three columns for display.

Community
  • 1
  • 1
dperish
  • 1,493
  • 16
  • 27
  • I'm looking for a code like this: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [ID = "string1" + "string2" + default.ToString()] public int ID { get; set; } my mean from "default" in code come back to the value that get from (DatabaseGeneratedOption.Identity). – Muhammad Ehsan Mirzaei Sep 18 '16 at 19:46