AspnetUsersinroles Partial Class

This is the base partial data object class for the “usersinroles” table in the provider database. Using this class implies that there will be a singular relationship between the remaining provider tables and relevant data objects.

[csharp]

//*****************************************************************************
// This file is part of the data access layer example to the ASP.NET 2.0 provider database
// This file was written by Adam Buenz [WSS MVP] of ARB Security Solutions, LLC
// http://www.sharepointsecurity.com
//
// This file and its parts is free for re-distribution, for use in both free
// and commercial applications, however this header must remain intact for legal
// use. The data access layer example is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//*****************************************************************************
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Aspnet.Provider.Datalayer.Commands;
using Aspnet.Provider.Datalayer.DataTransferObjects;

namespace Aspnet.Provider.Datalayer
{
///

/// The AspnetUsersinroles class.
///

public partial class AspnetUsersinroles : IPersistable
{
#region Members

private bool isNew;
private bool _isNew;
private Guid userid;
private Guid roleid;

#endregion

#region Properties

///

/// The Userid.
///

public virtual Guid Userid
{
get { return this.userid; }
set { this.userid = value; }
}

///

/// The Roleid.
///

public virtual Guid Roleid
{
get { return this.roleid; }
set { this.roleid = value; }
}

#endregion

#region ColumnNames

///

/// The corresponding schema name.
///

internal const string SchemaName = “dbo”;

///

/// The corresponding table name.
///

internal const string TableName = “aspnet_UsersInRoles”;

///

/// The column names.
///

internal class ColumnNames
{
///

/// The column name of the Userid property.
///

public const string Userid=”UserId”;
///

/// The column name of the Roleid property.
///

public const string Roleid=”RoleId”;
}

#endregion

///

/// The default constructor.
///

public AspnetUsersinroles()
{
this.isNew = true;
PersistenceManager.InvokeInstanceMethod(this, “CreationComplete”, null);
}

///

/// The constructor of the required fields.
///

/// The Userid. /// The Roleid. public AspnetUsersinroles(Guid userid, Guid roleid)
{
this.userid = userid;
this.roleid = roleid;

this.isNew = true;
PersistenceManager.InvokeInstanceMethod(this, “CreationComplete”, null);
}

///

/// The constructor from IDataReader.
///

/// An initalized IDataReader. internal AspnetUsersinroles(IDataReader reader)
{
if ((reader[“UserId”] != null) && (reader[“UserId”] != DBNull.Value))
this.userid = (Guid)reader[“UserId”];
if ((reader[“RoleId”] != null) && (reader[“RoleId”] != DBNull.Value))
this.roleid = (Guid)reader[“RoleId”];

this.isNew = false;
}

///

/// Creates an IDbCommand to insert an object into the database.
///

/// An initialized IDbCommand object.
internal virtual IDbCommand CreateInsertCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “insert into [dbo].[aspnet_UsersInRoles] ([UserId], [RoleId]) values (@userid, @roleid)”;
cmd.Parameters.AddWithValue(“@userid”, this.userid);
cmd.Parameters.AddWithValue(“@roleid”, this.roleid);

cmd.Connection = PersistenceManager.Connection;
return cmd;
}

///

/// Creates an IDbCommand to update an object in the database.
///

/// An initialized IDbCommand object.
internal virtual IDbCommand CreateUpdateCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “update [dbo].[aspnet_UsersInRoles] set where ([UserId]=@userid) and ([RoleId]=@roleid)”;
cmd.Parameters.AddWithValue(“@userid”, this.userid);
cmd.Parameters.AddWithValue(“@roleid”, this.roleid);

cmd.Connection = PersistenceManager.Connection;
return cmd;
}

///

/// Creates an IDbCommand to delete an object in the database.
///

/// An initialized IDbCommand object.
internal virtual IDbCommand CreateDeleteCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “delete from [dbo].[aspnet_UsersInRoles] where ([UserId]=@userid) and ([RoleId]=@roleid)”;
cmd.Parameters.AddWithValue(“@userid”, this.userid);
cmd.Parameters.AddWithValue(“@roleid”, this.roleid);

cmd.Connection = PersistenceManager.Connection;
return cmd;
}

///

/// Persists the object.
///

public virtual void Persist()
{
PersistenceManager.InvokeInstanceMethod(this, “PrePersist”, null);

IDbCommand cmd;

if (this.isNew)
cmd = this.CreateInsertCommand();
else
cmd = this.CreateUpdateCommand();

cmd.Transaction = PersistenceManager.Transaction;
PersistenceManager.RegisterInTransaction(this);

bool connWasClosed =
PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
PersistenceManager.Connection.State.Equals(ConnectionState.Broken);

try
{
if (connWasClosed)
PersistenceManager.Connection.Open();

cmd.ExecuteNonQuery();

this.isNew = false;

if (connWasClosed)
PersistenceManager.Connection.Close();
}
catch (Exception ex)
{
if (connWasClosed)
PersistenceManager.Connection.Close();
throw ex;
}
}

///

/// Deletes the object.
///

public virtual void Delete()
{
PersistenceManager.InvokeInstanceMethod(this, “PreDelete”, null);

IDbCommand cmd = this.CreateDeleteCommand();
cmd.Transaction = PersistenceManager.Transaction;

bool connWasClosed =
PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
PersistenceManager.Connection.State.Equals(ConnectionState.Broken);

try
{
if (connWasClosed)
PersistenceManager.Connection.Open();

cmd.ExecuteNonQuery();

if (connWasClosed)
PersistenceManager.Connection.Close();
}
catch (Exception ex)
{
if (connWasClosed)
PersistenceManager.Connection.Close();
throw ex;
}
}

///

/// Retrieves a AspnetUsersinroles object by its primary key (Throws System.DataException).
///

/// The Userid. /// The Roleid. /// The AspnetUsersinroles object.
public static AspnetUsersinroles Get(Guid userid, Guid roleid)
{
AspnetUsersinroles aspnetusersinroles;
if (AspnetUsersinroles.TryGet(userid, roleid, out aspnetusersinroles))
return aspnetusersinroles;
else
throw new DataException(“‘AspnetUsersinroles’ object not found.”);
}

///

/// Tries to retrieve a AspnetUsersinroles object by its primary key.
///

/// The Userid. /// The Roleid. /// The found AspnetUsersinroles or null if the primary key value does not exist. /// True if the AspnetUsersinroles exists, else false.
public static bool TryGet(Guid userid, Guid roleid, out AspnetUsersinroles aspnetusersinroles)
{
SqlCommand cmd = new SqlCommand(“select * from [dbo].[aspnet_UsersInRoles] where ([UserId]=@userid) and ([RoleId]=@roleid)”);
cmd.Parameters.AddWithValue(“@userid”, userid);
cmd.Parameters.AddWithValue(“@roleid”, roleid);

IList list = AspnetUsersinroles.Query(cmd);

if (list.Count == 0)
{
aspnetusersinroles = null;
return false;
}
else
{
aspnetusersinroles = list[0];
return true;
}
}

///

/// For internal use only.
///

public virtual void SaveState()
{
this._isNew = this.isNew;
}

///

/// For internal use only.
///

public virtual void RestoreState()
{
this.isNew = this._isNew;
}

///

/// Refreshes the internal state of the object.
///

/// This method should be called after xml deserialization to refresh internal flags.
public virtual void RefreshState()
{
AspnetUsersinroles aspnetusersinroles;

if (AspnetUsersinroles.TryGet(this.userid, this.roleid, out aspnetusersinroles))
{
this.isNew = false;
}
else
{
this.isNew = true;
}
}

///

/// Returns a Data Transfer Object of this AspnetUsersinroles.
///

/// A Data Transfer Object of this AspnetUsersinroles.
public virtual AspnetUsersinrolesDTO GetDTO()
{
AspnetUsersinrolesDTO dto = new AspnetUsersinrolesDTO();

dto.Userid = this.Userid;
dto.Roleid = this.Roleid;

return dto;
}

///

/// Applies a Data Transfer Object data to this AspnetUsersinroles.
///

/// The Data Transfer Object. public virtual void SetDTO(AspnetUsersinrolesDTO dto)
{
this.Userid = dto.Userid;
this.Roleid = dto.Roleid;
}

///

/// Perfoms a query on AspnetUsersinroles objects.
///

/// An IDbCommand containing the select statement. /// A result list of AspnetUsersinroles objects.
internal static IList Query(IDbCommand command)
{
command.Connection = PersistenceManager.Connection;
command.Transaction = PersistenceManager.Transaction;

bool connWasClosed =
PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
PersistenceManager.Connection.State.Equals(ConnectionState.Broken);

try
{
if (connWasClosed)
PersistenceManager.Connection.Open();

List list = new List();
IDataReader reader = command.ExecuteReader();

using (reader)
{
while (reader.Read())
list.Add(new AspnetUsersinroles(reader));
}

if (connWasClosed)
PersistenceManager.Connection.Close();

return list;
}
catch (Exception ex)
{
if (connWasClosed)
PersistenceManager.Connection.Close();
throw ex;
}
}

///

/// Finds all AspnetUsersinroles objects with a certain Userid value.
///

/// The Userid value. /// All AspnetUsersinroles objects with a certain Userid value.
public static IList FindByUserid(Guid userid)
{
return AspnetUsersinroles.Query(AspnetUsersinrolesCommands.FindByUserid(userid));
}

///

/// Finds all AspnetUsersinroles objects with a certain Roleid value.
///

/// The Roleid value. /// All AspnetUsersinroles objects with a certain Roleid value.
public static IList FindByRoleid(Guid roleid)
{
return AspnetUsersinroles.Query(AspnetUsersinrolesCommands.FindByRoleid(roleid));
}

}
}

 [/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *