How to set a string as nullable entity framework

c# entity-framework

Question

I'm getting this exception:

The model backing the 'database' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

As I have been told, that error comes when you change you model that represents the tables of you database and your database have not the same attributes(tables and fields)(correct me if I'm wrong).

Tbl_Users, which is in the database, has the following structure

dbUser(int,not null)//key
dbUserId(varchar(50),null)//allow null, right?
dbPassWord(varchar(20),null)

This is the model:

public class Tbl_Users {
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int dbUser { get; set; }
    [MaxLength(50)]
    public string dbPassWord { get; set; }
    [MaxLength(20)]
    public string dbUserID{ get; set; }
    public int? dbLock { get; set; }
}

How can I set public string dbPassWord { get; set; } and public string dbPassWord { get; set; } to allow nulls? Or is there any other solution for this error?

Update

If I add a migration, and I update the database with Update-Database as told here, will the values of the affected table be erased?

1
1
6/5/2014 11:53:57 AM

Accepted Answer

If i add a migration, and i update the database with Update-Database command as told here. Will the values of the afected table be erase?

Changing column to 'nullable' (providing it's not an index) or adding new columns (your dbLock) - should not delete data. And most of the time EF/CF migrations are to be trusted to do just the minimal changes on your existing Db structure - of course depending on what you did change (I'd be careful with renaming etc.).

Anyways, do the Add-Migration - and look up the migration file created (.cs - opens automatically for you) - it tells you what's in the up and down sections. You can recheck here what's EF doing for you.

And as suggested, backup first - and you're safe.

Other than that - even though Code First (via migrations) is made to adapt to changes to your objects - it's to be used during prototyping and development mostly - and minimize such changes in any production environment, as things will get more problematic.

0
3/25/2013 9:26:25 PM

Popular Answer

As Jon Skeet's comment string is a nullable type.

public string MyProperty{get;set;}

MyProperty = null; //this is fine


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow