Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 4.22 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Entity framework model generating weird error message
  2. CREATE TABLE security.aspnet_users (
  3.     userid UUID NOT NULL,
  4.     applicationname VARCHAR(255) NOT NULL,
  5.     username VARCHAR(255),
  6.     lastactivitydate TIMESTAMP,
  7.     isanonymous INT,
  8.     CONSTRAINT aspnet_users_userid_pk PRIMARY KEY (userid),
  9.     CONSTRAINT aspnet_users_username_pk UNIQUE (username, applicationname)
  10. );
  11.  
  12. CREATE TABLE security.aspnet_membership (
  13.     userid UUID NOT NULL,
  14.     password VARCHAR(255),
  15.     passwordsalt VARCHAR(255),
  16.     passwordformat INT,
  17.     email VARCHAR(255),
  18.     passwordquestion VARCHAR(255),
  19.     passwordanswer VARCHAR(255),
  20.     comments VARCHAR(255),
  21.     isapproved INT,
  22.     islockedout INT,
  23.     creationdate TIMESTAMP,
  24.     lastlogindate TIMESTAMP,
  25.     lastpasswordchangeddate TIMESTAMP,
  26.     lastlockoutdate TIMESTAMP,
  27.     failedpasswordattemptcount INT,
  28.     failedpasswordattemptstart TIMESTAMP,
  29.     failedpasswordanswercount INT,
  30.     failedpasswordanswerstart TIMESTAMP,
  31.  
  32.     CONSTRAINT aspnet_membership_userid_pk PRIMARY KEY (userid),
  33.     CONSTRAINT aspnet_membership_userid_ref FOREIGN KEY (userid) REFERENCES security.aspnet_users (userid)
  34. );
  35.  
  36. CREATE TABLE security.aspnet_profiles (
  37.     userid                      UUID,
  38.     propertynames               BYTEA NOT NULL,
  39.     propertyvaluesstring        BYTEA NOT NULL,
  40.     propertyvaluesbinary        BYTEA NULL,
  41.     lastupdateddate             TIMESTAMP NOT NULL,
  42.  
  43.     CONSTRAINT aspnet_profiles_userid_ref FOREIGN KEY (userid) REFERENCES security.aspnet_users (userid),
  44.     CONSTRAINT aspnet_profiles_userid_key UNIQUE (userid)
  45. );
  46.        
  47. public static void SaveUserProfile( CarSystemEntities context, UserProfileData data ) {
  48.     if ( context == null )
  49.         throw new ArgumentNullException( "context", "You must pass a non-null CarSystemEntities instance." );
  50.  
  51.     if ( data == null )
  52.         throw new ArgumentNullException( "data", "You must pass a non-null UserProfileData instance." );
  53.  
  54.     try {
  55.         Profile profile = null;
  56.  
  57.         if ( !context.Profiles.Any( p => p.Userid == data.ID ) ) {
  58.             profile = new CarSystem.Profile{
  59.                 LastUpdatedDate      = data.LastUpdatedDate.LocalDateTime,
  60.                 PropertyNames        = data.PropertyNames,
  61.                 PropertyValuesBinary = data.PropertyValuesBinary,
  62.                 PropertyValuesString = data.PropertyValuesString,
  63.                 Userid               = data.ID
  64.             };
  65.  
  66.             context.Profiles.AddObject( profile );
  67.         } else {
  68.             profile = QueryUerProfiles( context ).Where( p => p.Userid == data.ID ).Single();
  69.  
  70.             if ( profile.LastUpdatedDate      != data.LastUpdatedDate )      profile.LastUpdatedDate      = data.LastUpdatedDate.LocalDateTime;
  71.             if ( profile.PropertyNames        != data.PropertyNames )        profile.PropertyNames        = data.PropertyNames;
  72.             if ( profile.PropertyValuesBinary != data.PropertyValuesBinary ) profile.PropertyValuesBinary = data.PropertyValuesBinary;
  73.             if ( profile.PropertyValuesString != data.PropertyValuesString ) profile.PropertyValuesString = data.PropertyValuesString;
  74.         }
  75.  
  76.     } catch ( Exception ex ) {
  77.         throw new DataAccessException( DataAccessOperation.SaveProfile, FailureReason.DatabaseError, ex );
  78.     }
  79. }
  80.        
  81. if ( !context.Profiles.Any( p => p.Userid == data.ID ) ) {
  82.             profile = new CarSystem.Profile{
  83.                 LastUpdatedDate      = data.LastUpdatedDate.LocalDateTime,
  84.                 PropertyNames        = data.PropertyNames,
  85.                 PropertyValuesBinary = data.PropertyValuesBinary,
  86.                 PropertyValuesString = data.PropertyValuesString,
  87.                 Userid               = data.ID
  88.  
  89.                 //add the next line to ensure there is a pk field
  90.                 ID                   = Guid.NewGuid()
  91.             };
  92.        
  93. using ( CarSystemEntities context = new CarSystemEntities() ) {
  94.     if ( context.Connection.State != ConnectionState.Open ) {
  95.         context.Connection.Open();
  96.     }
  97.  
  98.     DbTransaction transaction = context.Connection.BeginTransaction();
  99.  
  100.     try {
  101.         <Data processing code here>
  102.  
  103.         transaction.Commit();
  104.  
  105.     } catch ( Exception ex ) {
  106.         transaction.Rollback();
  107.  
  108.         <More exception code here as needed>
  109.     }            
  110. }