i trying setup product key system in application, want ensure attribute has right size (16 characters).
i tried following
public class productkey { public const int productkeylength = 16; [stringlength(productkeylength, minimumlength = productkeylength)] private string _value; [required] [index(isunique = true)] public string value { { var temp = regex.replace(this._value, ".{4}", "$0-"); return temp.trim('-'); } set { this._value = value.replace("-", "");} } }
i want enable user insert key our without hyphen. following error above code:
column 'value' in table 'dbo.productkeys' of type invalid use key column in index.
as understood, need set limit value can used unique key. but, _value has limit , _value actual representation of value in database.
is there way set limit correctly in case?
thanks in advance.
you getting error because without stringlength
attribute on value
field, database column gets created varchar(max)
cannot used key. need [stringlength]
on field being used key. however, getter returning key formatted dashes, need key length 19:
public class productkey { public const int productkeylength = 19; private string _value { get; set; } [key] [required] [stringlength(productkeylength, minimumlength = productkeylength)] [index(isunique = true)] public string value { { var temp = regex.replace(this._value, ".{4}", "$0-"); return temp.trim('-'); } set { this._value = value.replace("-", ""); } } }
you might better off doing format conversion in viewmodels , client-side code, 1 problem you'll have here searching - example...
db.keys.add(new productkey { value = "1234-5678-9012-3456" }); db.keys.add(new productkey { value = "1234567890123455" }); db.savechanges(); console.writeline(db.keys.count(k => k.value.contains("89"))); // 0 console.writeline(db.keys.count(k => k.value.contains("8-9"))); // 2