Daces Zariņas emuāri

Entity Framework un kolonu vērtības pēc noklusējuma

Šodien saskāros ar diezgan interesantu problēmu - kā izdarīt tā, lai saglabājot datus datu bāzē, izmantojot Entity Framework, nenorādot konkrētas kolonas vērtību, tai tiktu piešķirta vērtība pēc noklusējuma, kas ir definēta datu bāzē, nevis tiek paņemta konkrētā tipa vērtība pēc noklusējuma?

Piem.,

mums datu bāzē ir definēta šāda tabula:

CREATE TABLE DEAL(

[ID] [bigint]  NOT NULL,

[NAME] [varchar](50) NOT NULL ,

[MIN_AMOUNT][bigint] DEFAULT (1),

[DATE] [datetime] NOT NULL);

Kods, lai saglabātu DEALS objektu datu bāzē ir apmēram šāds:

using (var cnt = new Context())

{

DEAL deal = new DEAL();

deal.NAME = "The new Deal";

cnt.DEAL.AddObject(deal);

cnt.SaveChanges();

}

 

Taču pēc tam, apskatoties datu bāzē, lai gan es kodā nebiju norādījusi MIN_AMOUNT vērtību, MIN_AMOUNT ir 0 (kas ir tipa vērtība pēc noklusējuma) nevis 1 (kā ir definēts datu bāzes līmenī).

Izrādās, ka risinājums ir pavisam vienkāršs... Vietā, kur notiek DB kolonu mapošana uz objektiem, ir nepieciešams pamainīt dažus uzstādījumus.

Ir nepieciešams uzlikt StoreGeneratedPattern uz Computed.

Un nākošo reizi, kad tiks saglabāti dati datu bāzē MIN_AMOUNT, kolonai tiks piešķirta vērtība pēc noklusējuma, kas ir definēta datu bāzē. 

Cerams, ka noderēs :)

Comments

crork said:

XskKdX Really appreciate you sharing this article post.Much thanks again. Fantastic.

# October 7, 2012 3:23 PM

bookmarking service said:

JuPWds I really enjoy the article. Want more.

# November 9, 2012 7:58 PM

Seo Service said:

E04KgW Really enjoyed this article post.Much thanks again. Cool.

# November 28, 2012 8:22 AM

generic viagra said:

x1ryjo Great, thanks for sharing this blog article.Really looking forward to read more. Really Great.

# January 10, 2013 6:19 AM

submit to social bookmarking sites said:

A1CQb6 Appreciate you sharing, great blog post. Really Cool.

# January 17, 2013 9:08 PM

archeage gold said:

There is clearly a bundle to know about this.  I consider you made some nice points in features also. archeage gold www.cheap-archeage-gold.com

# February 27, 2013 12:53 AM

social bookmarks said:

2x7hek Major thanks for the blog post.Really looking forward to read more. Will read on...

# March 15, 2013 1:59 PM

social bookmarking service said:

6qBc4g Major thanks for the article.Thanks Again. Fantastic.

# March 22, 2013 5:45 PM

social bookmarking service said:

1Togn5 Thanks for the article.Really thank you! Really Cool.

# April 4, 2013 8:11 AM

stoner said:

I really like and appreciate your blog post.Really looking forward to read more. Cool.

# April 6, 2013 8:45 AM

buy social bookmarks said:

bSIViP I really liked your post.Really thank you! Really Cool.

# April 12, 2013 7:44 PM

buy social bookmarks said:

LIiSUt Enjoyed every bit of your blog post.Really thank you! Much obliged.

# April 16, 2013 9:07 AM

Segura said:

soin visage soin visage soin visage soin visage

# May 10, 2013 9:32 AM
Leave a Comment

(obligāts) 

(obligāts) 

(brīvizvēles)

(obligāts)