10

I have a model like this

public class MyModel
{
    public int MyModelId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Title { get; set; }
}

I was wondering if there's a way, using Data Annotations, to set the value of a property - say Title - default to other property value, i.e. Name. Something like:

if(MyModel.Title == "") MyModel.Title = MyModel.Name;
5
  • Possible Dupe: stackoverflow.com/questions/40730/… Commented Jun 8, 2011 at 1:43
  • I'm trying to achieve this with Data Annotations, though I don't think it's possible. Anyway I don't think it is a dupe. Commented Jun 8, 2011 at 1:47
  • it's not quite the same, but if you read the answers and comments you'll see that they answer your question Commented Jun 8, 2011 at 1:51
  • Yes but the answers are from 2008. EF went 4.1 this year. Something could be new. Commented Jun 8, 2011 at 1:58
  • 2
    Doesn't appear so msdn.microsoft.com/en-us/library/gg197525(VS.103).aspx Commented Jun 8, 2011 at 2:00

3 Answers 3

10

If you want default value set it in entity default (parameterless) constructor. There is no need to have data annotation for something which you can do directly.

Sign up to request clarification or add additional context in comments.

2 Comments

Default values are useful when adding a non-nullable column to an existing table with records in it. I was searching for a way to do it and came across this question - is there a way to specify the value to populate for all existing records?
@Crake this should probably be its own separate question, but I was able to do this with defaultValueSql in my data migration: AddColumn("ExistingTable", "NewColumn",c => c.Int(nullable: false, defaultValueSql: "0")); (change type and default value as necessary)
4

you can tell entity framework that database will take care of that property by editing that property in SSDL of the edmx file.

Initially

<Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" />

we have change it to

<Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" StoreGeneratedPattern="Computed" />

by setting storeGeneratedPattern="Computed" we can tell to EF that the property value will be inserted by DB.

For Editing SSDL

  1. Right click the edmx file, open with XML(text) Editor.

2.Ctrl+F name of the property and just change that property

I don't know is there a way to do with data annotations.

Comments

0

It's impossible to set attribute argument as a function in general, you will get an error:

"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.