locked
RIA Service problem!!! RRS feed

  • Question

  • Hello,

    The simplified scenario:

    Entities

    Data source

    Porperties

    Companies

    Intrinsic

    Id

    Name

    CEO

    Phone

    etc…

    Persons

    Attached

    Id

    FirstName

    MiddleName

    LastName1

    LastName2

    Notices

    Intrinsic

    Id

    Company

    Person

    Body

    etc…

    I needed a RIA Service to eliminate many unneeded columns in the Notices entity.

    public class NoticeSimplified

        {  

            [Key]

            public int Id { get; set; }

            public string PersonPreferredName { get; set; }

        }

    The problem:

    To get the person's preferred name I need to access the attached data source too. How can I do it?




    • Edited by san_pedro Saturday, May 9, 2015 5:54 PM
    Saturday, May 9, 2015 5:44 PM

Answers

  • You have to wrap the second connection in TransactionScope

        string connString =
                System.Web.Configuration.WebConfigurationManager
                .ConnectionStrings["OtherData"].ConnectionString;
    
        using (SqlConnection connection = new SqlConnection(connString))
        {
            using (TransactionScope scope
                = new TransactionScope(TransactionScopeOption.Suppress))
            {
    
                StringBuilder MainQuery = new StringBuilder();
    
                MainQuery.Append(String.Format("Select or Update..."));
    
                SqlCommand command = new SqlCommand(MainQuery.ToString(), connection);
                command.CommandType = CommandType.Text;
    
                connection.Open();
                Int32 recordsAffected = command.ExecuteNonQuery();
            }
        }



    Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Angie Xu Monday, May 18, 2015 1:32 AM
    Friday, May 15, 2015 5:58 PM

All replies

  • A specific forum for WCF RIA Services is more suitable, for example: WCF RIA Services with Silverlight forum
    Monday, May 11, 2015 12:51 PM
  • You can attach RIA services to external datasources.  See my reply to this post for details.  If you need both data sources you will need to supply the connection string information for both, but it should be possible.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/a4851a6b-274c-4923-9645-dd6e973783fa/cant-connect-to-ria-service-in-lightswitch-application?forum=lightswitch

    • Edited by Hessc Tuesday, May 12, 2015 2:35 AM
    Tuesday, May 12, 2015 2:35 AM
  • Thank’s Hessc for your response.

    Based on Michael’s tutorial (http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/2226/Creating-a-WCF-RIA-Service-for-Visual-Studio-2013.aspx) I’ve done this:

    private ApplicationData ad_context;
            public ApplicationData adContext
            {
                get
                {
                    if (this.ad_context == null)
                    {
                        var connString =
                            System.Web.Configuration.WebConfigurationManager
                            .ConnectionStrings["_IntrinsicData"].ConnectionString;
                        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                        builder.Metadata =
                            "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
                        builder.Provider =
                            "System.Data.SqlClient";
                        builder.ProviderConnectionString = connString;
                        this.ad_context = new ApplicationData(builder.ConnectionString);
                    }
                    return this.ad_context;
                }
            }
    
            private AttachedData emd_context;
            public AttachedData emdContext
            {
                get
                {
                    if (this.emd_context == null)
                    {
                        string connString =
                            System.Web.Configuration.WebConfigurationManager
                            .ConnectionStrings["AttachedData "].ConnectionString;
                        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                        builder.Metadata =
                            "res://*/ AttachedData.csdl|res://*/ AttachedData.ssdl|res://*/ AttachedData.msl";
                        builder.Provider =
                            "System.Data.SqlClient";
                        builder.ProviderConnectionString = connString;
                        this.emd_context = new AttachedData (builder.ConnectionString);
                    }
                    return this.emd_context;
                }
            }
    

    It works fine if they are used separately. But when I try to get the person name (from the attached DB) of the Notice entity (intrinsic DB) I get this error:

    El código de usuario no controló System.Data.EntityException

      HResult=-2146233087

      Message= Error the underlying provider failed on open.

      Source=System.Data.Entity

      StackTrace:

           en System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)

           en System.Data.EntityClient.EntityConnection.Open()

           en System.Data.Objects.ObjectContext.EnsureConnection()

           en System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)

           en System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()

           en System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)

           en System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)

           en System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)

           en System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)

           en System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)

           en WCF_RIA_Project.WCF_RIA_Service.NombrePreferido(Int32 IdPersona)

           en WCF_RIA_Project.WCF_RIA_Service.GetCartelera()

           en GetCartelera(DomainService , Object[] )

           en System.ServiceModel.DomainServices.Server.ReflectionDomainServiceDescriptionProvider.ReflectionDomainOperationEntry.Invoke(DomainService domainService, Object[] parameters)

           en System.ServiceModel.DomainServices.Server.DomainOperationEntry.Invoke(DomainService domainService, Object[] parameters, Int32& totalCount)

           en System.ServiceModel.DomainServices.Server.DomainService.Query(QueryDescription queryDescription, IEnumerable`1& validationErrors, Int32& totalCount)

      InnerException: System.Transactions.TransactionAbortedException

           HResult=-2146233087

           Message=Se anuló la transacción.

           Source=System.Transactions

           StackTrace:

                en System.Transactions.TransactionStateAborted.CheckForFinishedTransaction(InternalTransaction tx)

                en System.Transactions.EnlistableStates.Promote(InternalTransaction tx)

                en System.Transactions.Transaction.Promote()

                en System.Transactions.TransactionInterop.ConvertToOletxTransaction(Transaction transaction)

                en System.Transactions.TransactionInterop.GetExportCookie(Transaction transaction, Byte[] whereabouts)

                en System.Data.SqlClient.SqlInternalConnection.GetTransactionCookie(Transaction transaction, Byte[] whereAbouts)

                en System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)

                en System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)

                en System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction transaction)

                en System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)

                en System.Data.ProviderBase.DbConnectionPool.PrepareConnection(DbConnection owningObject, DbConnectionInternal obj, Transaction transaction)

                en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)

                en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)

                en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)

                en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)

                en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)

                en System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)

                en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)

                en System.Data.SqlClient.SqlConnection.Open()

                en System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)

           InnerException: System.Transactions.TransactionPromotionException

                HResult=-2146233087

                Message= Error trying to promote the transaction.

                Source=System.Data

                StackTrace:

                     en System.Data.SqlClient.SqlDelegatedTransaction.Promote()

                     en System.Transactions.TransactionStatePSPEOperation.PSPEPromote(InternalTransaction tx)

                     en System.Transactions.TransactionStateDelegatedBase.EnterState(InternalTransaction tx)

                InnerException: System.Data.SqlClient.SqlException

                     HResult=-2146232060

                     Message=MSDTC on server 'PEDRO\LOCALDB#64F7B050' is unavailable.

                     Source=.Net SqlClient Data Provider

                     ErrorCode=-2146232060

                     Class=16

                     LineNumber=1

                     Number=8501

                     Procedure=""

                     Server=(LocalDB)\v11.0

                     State=3

                     StackTrace:

                          en System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

                          en System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)

                          en System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)

                          en System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

                          en System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)

                          en System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)

                          en System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)

                          en System.Data.SqlClient.SqlDelegatedTransaction.Promote()

                     InnerException:

    Tuesday, May 12, 2015 9:14 PM
  • Any idea?

    Friday, May 15, 2015 5:45 PM
  • You have to wrap the second connection in TransactionScope

        string connString =
                System.Web.Configuration.WebConfigurationManager
                .ConnectionStrings["OtherData"].ConnectionString;
    
        using (SqlConnection connection = new SqlConnection(connString))
        {
            using (TransactionScope scope
                = new TransactionScope(TransactionScopeOption.Suppress))
            {
    
                StringBuilder MainQuery = new StringBuilder();
    
                MainQuery.Append(String.Format("Select or Update..."));
    
                SqlCommand command = new SqlCommand(MainQuery.ToString(), connection);
                command.CommandType = CommandType.Text;
    
                connection.Open();
                Int32 recordsAffected = command.ExecuteNonQuery();
            }
        }



    Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Angie Xu Monday, May 18, 2015 1:32 AM
    Friday, May 15, 2015 5:58 PM
  • Thank you very much Michael!!!
    Wednesday, May 20, 2015 7:21 PM