SELECT TableName = t.NAME, IndexName = ind.NAME, IndexId = ind.index_id, ColumnId = ic.index_column_id, ColumnName = col.NAME, ind.*, ic.*, col.* FROM sys.indexes ind INNER JOIN sys.index_columns ic ON ind.object_id = ic.object_id AND ind.index_id = ic.index_id INNER JOIN sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id INNER JOIN sys.tables t ON ind.object_id = t.object_id WHERE ind.is_primary_key = 0 AND ind.is_unique = 0 AND ind.is_unique_constraint = 0 AND t.is_ms_shipped = 0 AND t.NAME = 'Order' -- Replace with the table name that you want to check ORDER BY t.NAME, ind.index_id, ind.NAME, ic.index_column_id;
Thursday, May 3, 2018
Get a list of all index & index columns in SQL Server
Monday, October 2, 2017
Re-publish Web App to Azure from Visual Studio 2017
It's easy to download the published profile from the Azure portal if your application has already been published. Here is how you can download the publishing profile from the Azure portal and use that to publish your application from Visual Studio.
1. log in to the Azure dashboard and open-up the App service that you want to publish your application.
2. Download the publish profile.
3. Go to your Web application in Visual Studio and get the Publishing wizard.
4. Select Import Profile and click on Publish. You will be prompted to select the Imported Publish Setting File.
If you are connected with the Azure Subscription, you can use the server explorer panel to access the published files in case you need to alter any file contents without login into Kudu services.
1. log in to the Azure dashboard and open-up the App service that you want to publish your application.
2. Download the publish profile.
3. Go to your Web application in Visual Studio and get the Publishing wizard.
4. Select Import Profile and click on Publish. You will be prompted to select the Imported Publish Setting File.
If you are connected with the Azure Subscription, you can use the server explorer panel to access the published files in case you need to alter any file contents without login into Kudu services.
Friday, June 2, 2017
View all Foreign Key Relationships in the Database
I've found some good directions from stack-overflow. Combining a few solutions, I could get the result that I wanted.
Query 1 :
Query 2 :
Query 1 :
SELECT RC.constraint_name AS ForeignKey,
KF.table_name AS TableName,
KF.column_name AS ColumnName,
KP.table_name AS ReferenceTableName,
KP.column_name PK_Column,
RC.match_option MatchOption,
RC.update_rule UpdateRule,
RC.delete_rule DeleteRule
FROM information_schema.referential_constraints RC
JOIN information_schema.key_column_usage KF
ON RC.constraint_name = KF.constraint_name
JOIN information_schema.key_column_usage KP
ON RC.unique_constraint_name = KP.constraint_name
KF.table_name AS TableName,
KF.column_name AS ColumnName,
KP.table_name AS ReferenceTableName,
KP.column_name PK_Column,
RC.match_option MatchOption,
RC.update_rule UpdateRule,
RC.delete_rule DeleteRule
FROM information_schema.referential_constraints RC
JOIN information_schema.key_column_usage KF
ON RC.constraint_name = KF.constraint_name
JOIN information_schema.key_column_usage KP
ON RC.unique_constraint_name = KP.constraint_name
SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
Friday, January 27, 2017
Exclude files from getting compiled in Visual Studio Project Depending on the Platform Configuration
I had to include and maintain some SAP Business One system forms captured from each SAP Business One version (9.0, 9.1, and 9.2) for an SAP Business One Add-on Visual Studio project. Of course, I've tried with placing them in separate folders, but during the compilation of the source code, I've got this build error.
So, I thought of an out-of-the-box solution that could help me to compile my add-on project with only the source code files that are related to the SAP version and bit version. This was the summary of the R&D work that I did.
Problem : How can I explicitly exclude/include source code files in Visual Studio, depend on the Solution Configuration or Solution Platform.
Directions :
I've created a sample visual studio solution and added two empty classes.
Then I've opened up the ConsoleApplication1 project file using a text editor.
Then Noticed the usage of the Condition element and moved my Class1.cs and Class2.cs in to two separate item groups as below.
Now, When I switch the value to 32bit from the Solution platform, only Class1 is getting attached to the compiler.
Similarly, for 64bit, I only get Class2.
Since both Class1 and Class2 coexist in the solution at any time, this workaround will definitely help to get rid of build automation-related work, even if it's hard to maintain.
So, I thought of an out-of-the-box solution that could help me to compile my add-on project with only the source code files that are related to the SAP version and bit version. This was the summary of the R&D work that I did.
Problem : How can I explicitly exclude/include source code files in Visual Studio, depend on the Solution Configuration or Solution Platform.
Directions :
I've created a sample visual studio solution and added two empty classes.
Then I've opened up the ConsoleApplication1 project file using a text editor.
Then Noticed the usage of the Condition element and moved my Class1.cs and Class2.cs in to two separate item groups as below.
Now, When I switch the value to 32bit from the Solution platform, only Class1 is getting attached to the compiler.
Similarly, for 64bit, I only get Class2.
Since both Class1 and Class2 coexist in the solution at any time, this workaround will definitely help to get rid of build automation-related work, even if it's hard to maintain.
Friday, December 2, 2016
Entity Framework Code First with Repository Pattern
Problem: Need to develop a back-end (Data Layer and Business Logic Layer) of a data-driven application with less effort. Both layers must be able to unit test.
Solution: Implement data model using Entity Framework with code first approach and implement Data access layer using generic repository pattern. On top of that, implement a unit of work pattern to create an abstraction layer in between the data access layer and the business logic layer.
Sample Code :
Two class library-type projects have been added to the solution, One project contains the entity classes and the other project contains the model (Project reference has been added from the Entity project to the Model project). A code-first entity model is added to the model project and code-first entity classes are created in the entity project.
Ex: Sample code first entity class
using System.ComponentModel.DataAnnotations.Schema; namespace MyDevTalks.Entities { [Table("Customers")] public class Customer { public Guid Id { get; set; }
public string Name { get; set; }
} }
Sample Model class :
namespace MyDevTalks.Model { using MyDevTalks.Entities; using System.Data.Entity; public class Context : DbContext { public Context() : base("name=DataModel") { } public virtual DbSet<Customer> Customers { get; set; } } }
A generic interface class IGenericRepository<T> containing the CRUD operations that are common to all the entities in the model is added to the Model project.
using System; using System.Linq; using System.Linq.Expressions; namespace MyDevTalks.Model { public interface IGenericRepository<T> where T : class { IQueryable<T> GetAll(); IQueryable<T> FindBy(Expression<Func<T, bool>> predicate); void Add(T entity); void Delete(T entity); void Edit(T entity);
void Save(); } }
Then IGenericRepository<T> interface is implemented in a generic abstract class,
using System; using System.Data.Entity; using System.Linq;
using System.Linq.Expressions;
namespace MyDevTalks.Model { public abstract class GenericRepository<C, T> : IGenericRepository<T> where T : class where C : DbContext, new() { private C entities = new C();
public C Context { get { return entities; } set { entities = value; } }
public virtual IQueryable<T> GetAll() { IQueryable<T> query = entities.Set<T>(); return query; }
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate) { IQueryable<T> query = entities.Set<T>().Where(predicate); return query; }
public virtual void Add(T entity) { entities.Set<T>().Add(entity); }
public virtual void Delete(T entity) { entities.Entry(entity).State = EntityState.Deleted; entities.Set<T>().Remove(entity); }
public virtual void Edit(T entity) { entities.Entry(entity).State = EntityState.Modified; }
public virtual void Save() { entities.SaveChanges(); } } }
Then, made some concrete repository classes implementing the abstract GenericRepository.
using MyDevTalks.Entities; namespace MyDevTalks.Model.Repositories { public class CustomerRepository : GenericRepository<Context, Customer> { public void Foo(Guid Id){}
}}
Now I'm able to do all the CRUD operations + any concrete implementation.
[TestMethod] public void SomeTestMethod() {
// Dependancy inject CustomerRepository and ItemRepositoryContext myDbContext = new Context(); CustomerRepository _customerRepository= new CustomerRepository(myDbContext);
var cust = new Customer() { Id = Guid.NewGuid(), Name = "My Customer" }; _customerRepository.Add(cust); _customerRepository.Foo();
_customerRepository.Save();
//Some Other Repo
SomeOtherRepository _otherRepository = new SomeOtherRepository (myDbContext);_otherRepository .Foo(cust.Id);
_otherRepository .Save();
}
So the problem arises here when you have several repository classes, all of them will maintain their own instance of the Context class, which could result in data inconsistency, So usually the context object parsing is implemented at the Unit of Work classes, but it's better to make the default constructor of the Concrete Repository classes to private and implement an overloaded constructor, which accepts the Context. Also, Save() method has been removed from both IGenericRepository interface and GenericRepository abstract class.
using MyDevTalks.Entities; namespace MyDevTalks.Model.Repositories { public class CustomerRepository : GenericRepository<Context, Customer> { private CustomerRepository() { } public CustomerRepository(Context model) { base.Context = model; } public void Foo() { } } }
Accordingly, my test method should be altered as below. So the context is shared in between the repositories used in a single unit, in other words, a Unit of Work.
[TestMethod] public void SomeTestMethod() { Context myDbContext = new Context(); CustomerRepository custRep = new CustomerRepository(myDbContext); custRep.Add(new Entities.Customer() { Id = Guid.NewGuid(), Name = "My Customer" }); custRep.Foo(); //Some Other Repo SomeOtherRepository othrtRepo = new SomeOtherRepository (myDbContext); othrtRepo.Foo(custRep.Id);
myDbContext.SaveChanges(); }
Subscribe to:
Posts (Atom)




